From 41846af379d4d0ce00962aa86a97e2aee5855f4f Mon Sep 17 00:00:00 2001 From: lubega-deriv Date: Mon, 30 Sep 2024 12:20:29 +0800 Subject: [PATCH] chore: add dd-mm-yyyy format --- src/utils/__test__/format.utils.spec.ts | 6 ++++++ src/utils/format.utils.ts | 11 +++++++++-- utils-docs/docs/Utils/format.md | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/utils/__test__/format.utils.spec.ts b/src/utils/__test__/format.utils.spec.ts index e13e918..9319f7d 100644 --- a/src/utils/__test__/format.utils.spec.ts +++ b/src/utils/__test__/format.utils.spec.ts @@ -52,6 +52,12 @@ describe("FormatUtils.getFormattedDateString", () => { expect(formattedDate).toBe("2023-05-15"); }); + test('formats date string in "DD-MM-YYYY" format', () => { + const dateString = "2023-05-15T12:00:00Z"; + const formattedDate = getFormattedDateString(dateString, { format: "DD-MM-YYYY" }); + expect(formattedDate).toBe("15-05-2023"); + }); + test('formats date string in "DD MMM YYYY" format', () => { const dateString = "2023-05-15T12:00:00Z"; const formattedDate = getFormattedDateString(dateString, { format: "DD MMM YYYY" }); diff --git a/src/utils/format.utils.ts b/src/utils/format.utils.ts index c208a41..dcbe1c1 100644 --- a/src/utils/format.utils.ts +++ b/src/utils/format.utils.ts @@ -9,7 +9,7 @@ type FormatMoneyOptions = { type GetFormattedDateStringOptions = { dateOptions?: Intl.DateTimeFormatOptions; - format?: "YYYY-MM-DD" | "DD MMM YYYY" | "MMM DD YYYY"; + format?: "DD MMM YYYY" | "DD-MM-YYYY" | "MMM DD YYYY" | "YYYY-MM-DD"; unix?: boolean; }; @@ -61,7 +61,7 @@ export const formatMoney = (number: number, options?: FormatMoneyOptions) => { * - Can be a Date object, Unix timestamp, or date string. * @param {GetFormattedDateStringOptions} [options] - Optional configuration for date formatting, including: * - `dateOptions`: Intl.DateTimeFormatOptions to customize date formatting. - * - `format`: The desired output format. Supported formats: 'YYYY-MM-DD', 'DD MMM YYYY', 'MMM DD YYYY'. + * - `format`: The desired output format. Supported formats: 'YYYY-MM-DD', 'DD-MM-YYYY', 'DD MMM YYYY', 'MMM DD YYYY'. * - `unix`: If true, treats numeric input as a Unix timestamp. * * @returns {string} A formatted date string according to the specified format. @@ -118,6 +118,13 @@ export const getFormattedDateString = ( return dateObj .toLocaleDateString("en-GB", formattedDateOptions) .replace(/(\d{2}) (\w{3}) (\d{4})/, "$2 $1 $3"); + case "DD-MM-YYYY": + formattedDateOptions.day = "2-digit"; + formattedDateOptions.month = "2-digit"; + formattedDateOptions.year = "numeric"; + return dateObj + .toLocaleDateString("en-GB", formattedDateOptions) + .replace(/(\d{2})\/(\d{2})\/(\d{4})/, "$1-$2-$3"); default: formattedDateOptions.year = "numeric"; formattedDateOptions.month = "2-digit"; diff --git a/utils-docs/docs/Utils/format.md b/utils-docs/docs/Utils/format.md index 89fadb3..47052c5 100644 --- a/utils-docs/docs/Utils/format.md +++ b/utils-docs/docs/Utils/format.md @@ -56,7 +56,7 @@ Converts and formats a given date input into a specified string format. To be us `GetFormattedDateStringOptions [optional]` - Optional configuration for formatting the date value, including: - **dateOptions: [Intl.DateTimeFormatOptions-optional]**: - Optional configuration for date formatting. Defaults to `{ day: "2-digit", month: "2-digit", year: "numeric" }`. -- **format: [string-optional]**: The desired output format. Supported formats: `'YYYY-MM-DD'`, `'DD MMM YYYY'`, `'MMM DD YYYY'`. Defaults to `'YYYY-MM-DD'`. +- **format: [string-optional]**: The desired output format. Supported formats: `'YYYY-MM-DD'`, `'DD MMM YYYY'`, `'MMM DD YYYY'`, `'DD-MM-YYYY'`. Defaults to `'YYYY-MM-DD'`. - **unix: [boolean-optional]**: If `true`, treats the numeric input as a Unix timestamp. ### Returns: