Skip to content

Commit

Permalink
Merge pull request #80 from lubega-deriv/lubega-deriv/dd-mm-yyyy-format
Browse files Browse the repository at this point in the history
Lubega/chore: add dd-mm-yyyy format
  • Loading branch information
shayan-deriv authored Sep 30, 2024
2 parents 5749afe + 41846af commit 82203e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/utils/__test__/format.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
11 changes: 9 additions & 2 deletions src/utils/format.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion utils-docs/docs/Utils/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 82203e6

Please sign in to comment.