Skip to content

Commit

Permalink
feat: format date based on local and timezone (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-sharma7 authored Oct 18, 2024
1 parent e158104 commit c5b26c0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [Convert Time Zone](./guide/convertTimeZone.md)
- [Time Zone Offset Difference](./guide/getTimeZoneOffsetDifference.md)
- [Format Date in Time Zone](./guide/formatDateInTimeZone.md)
- [Format Date Based On Local](./guide/formatDateForLocale.md)
- [Calculate Duration](./guide/calculateDuration.md)

</details>
Expand Down
35 changes: 35 additions & 0 deletions docs/guide/formatDateForLocale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Format Date based on the given locale and timezone.

## Description

This feature allows you to format a date string based on the given locale and timezone.

## Usage

```javascript
import { formatDateForLocale } from 'world-clockify';

const formattedDateFR = formatDateForLocale('2024-10-17T10:30:00', 'fr', 'Europe/Paris');
const formattedDateUS = formatDateForLocale('2024-10-17T10:30:00', 'en-US', 'America/New_York');

console.log('Formatted Date (FR):', formattedDateFR);
console.log('Formatted Date (US):', formattedDateUS);
```

## Expected Output

```bash
Formatted Date (FR): jeudi 17 octobre 2024 à 12:30:00 heure d’Europe centrale
Formatted Date (US): Thursday, October 17, 2024, 06:30:00 AM Eastern Daylight Time

```

## Parameters

- dateStr (string): The date string in ISO format.
- local (string): The target local (e.g., 'fr', 'en-US').
- timezone (string): The format to use for the output date string.

## Returns

- (string): The formatted string in the target local.
29 changes: 29 additions & 0 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,35 @@ export const formatDateInTimeZone = (dateStr: string, fromZone: string, toZone:
return formattedDate;
};

/**
* Formats a date string based on the given locale and timezone.
*
* @param {string} dateStr - The date string in ISO format.
* @param {string} locale - The locale to format the date (e.g., 'fr', 'en-US').
* @param {string} timezone - The timezone for the date.
* @returns - The formatted date string.
*/

export const formatDateForLocale = (dateStr: string, locale: string, timezone: string): string => {
/* Create a DateTime object from the date string and timezone provided as arguments to the function.
The date string is expected to be in ISO format (e.g., '2022-01-01T12:00:00'). */
const date = DateTime.fromISO(dateStr, { zone: timezone });

if (!date.isValid) {
throw new Error(
`Invalid date string: "${dateStr}". Ensure the date is in ISO format (e.g., 'YYYY-MM-DDTHH:mm:ss').`,
);
}

if (!IANAZone.isValidZone(timezone)) {
throw new Error(
`Invalid timezone: "${timezone}". Please provide a valid IANA timezone (e.g., 'America/New_York').`,
);
}

return date.setLocale(locale).toLocaleString(DateTime.DATETIME_FULL);
};

/**
* Calculates the duration between two dates across timezones.
* @param {string} startDateStr - The start date in string format (ISO).
Expand Down
10 changes: 10 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getCurrentTimeInZone,
getTimeDifference,
formatDateInTimeZone,
formatDateForLocale,
calculateDuration,
getSupportedTimezones,
getSupportedCrrency,
Expand Down Expand Up @@ -41,6 +42,15 @@ describe('Timezone-Aware Date Helper', () => {
expect(result).toBe('10/14/2024 08:00');
});

it('should format date string based on the given locale and timezone.', () => {
const dateStr = '2024-10-17T10:30:00';
const local = 'en-US';
const timeZone = 'America/New_York';

const result = formatDateForLocale(dateStr, local, timeZone);
expect(result).toBe('October 17, 2024 at 10:30 AM EDT');
});

it('should handle other timezones and format correctly', () => {
const dateStr = '2024-10-14T12:00:00';
const fromZone = 'UTC';
Expand Down

0 comments on commit c5b26c0

Please sign in to comment.