Skip to content

Commit 60e3002

Browse files
naveen2593kumarNaveen KumarLFDanLusnowystinger
authored
fix-issues/4725: exported fromDate & fromEpoch and fixed the documentation (#4732)
* fix-issues/4725: exported fromDate & fromEpoch and fixed the documentations * Comment fix: using fromAbsolute and updated docs * Update packages/@internationalized/date/docs/ZonedDateTime.mdx Thanks. Indeed this is more intuitive now Co-authored-by: Daniel Lu <[email protected]> * Update packages/@internationalized/date/src/conversion.ts Co-authored-by: Robert Snow <[email protected]> * Update packages/@internationalized/date/src/conversion.ts Co-authored-by: Robert Snow <[email protected]> * Update packages/@internationalized/date/docs/ZonedDateTime.mdx Co-authored-by: Robert Snow <[email protected]> * Update packages/@internationalized/date/docs/ZonedDateTime.mdx Co-authored-by: Robert Snow <[email protected]> --------- Co-authored-by: Naveen Kumar <[email protected]> Co-authored-by: Daniel Lu <[email protected]> Co-authored-by: Robert Snow <[email protected]>
1 parent 99ca82e commit 60e3002

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

packages/@internationalized/date/docs/ZonedDateTime.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ let date = parseAbsolute('2021-11-07T07:45:00Z', 'America/Los_Angeles');
6161
let date = parseAbsoluteToLocal('2021-11-07T07:45:00Z');
6262
```
6363

64+
You can also create a `ZonedDateTime` using a `Date` object or epoch time (milliseconds) using one of the following functions:
65+
66+
* <TypeLink links={docs.links} type={docs.exports.fromDate} /> – This function creates a `ZonedDateTime` from a `Date` object. A time zone identifier, e.g. `America/Los_Angeles`, must be passed, and the result will be converted into that time zone.
67+
* <TypeLink links={docs.links} type={docs.exports.fromAbsolute} /> – This function creates a `ZonedDateTime` from a Unix epoch (e.g. `1688023843144`, representing milliseconds since 1970). A time zone identifier, e.g. `America/Los_Angeles`, must be provided, and the result will be converted into that time zone.
68+
69+
```tsx
70+
import {fromDate, fromAbsolute} from '@internationalized/date';
71+
72+
let date = fromDate(new Date(), 'America/Los_Angeles');
73+
let date = fromAbsolute(1688023843144, 'America/Los_Angeles');
74+
```
75+
76+
6477
The current time can be retrieved using the <TypeLink links={docs.links} type={docs.exports.now} /> function. This requires a time zone identifier to be provided, which is used to determine the local time. The <TypeLink links={docs.links} type={docs.exports.getLocalTimeZone} /> function can be used to retrieve the user's current time zone.
6578

6679
```tsx

packages/@internationalized/date/src/conversion.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ export function toDate(dateTime: CalendarDate | CalendarDateTime, timeZone: stri
173173
return new Date(toAbsolute(dateTime, timeZone, disambiguation));
174174
}
175175

176+
/**
177+
* Takes a Unix epoch (milliseconds since 1970) and converts it to the provided time zone.
178+
*/
176179
export function fromAbsolute(ms: number, timeZone: string): ZonedDateTime {
177180
let offset = getTimeZoneOffset(ms, timeZone);
178181
let date = new Date(ms + offset);
@@ -187,6 +190,9 @@ export function fromAbsolute(ms: number, timeZone: string): ZonedDateTime {
187190
return new ZonedDateTime(year, month, day, timeZone, offset, hour, minute, second, millisecond);
188191
}
189192

193+
/**
194+
* Takes a `Date` object and converts it to the provided time zone.
195+
*/
190196
export function fromDate(date: Date, timeZone: string): ZonedDateTime {
191197
return fromAbsolute(date.getTime(), timeZone);
192198
}

packages/@internationalized/date/src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ export {IslamicCivilCalendar, IslamicTabularCalendar, IslamicUmalquraCalendar} f
3838
export {HebrewCalendar} from './calendars/HebrewCalendar';
3939
export {EthiopicCalendar, EthiopicAmeteAlemCalendar, CopticCalendar} from './calendars/EthiopicCalendar';
4040
export {createCalendar} from './createCalendar';
41-
export {toCalendarDate, toCalendarDateTime, toTime, toCalendar, toZoned, toTimeZone, toLocalTimeZone} from './conversion';
41+
export {
42+
toCalendarDate,
43+
toCalendarDateTime,
44+
toTime,
45+
toCalendar,
46+
toZoned,
47+
toTimeZone,
48+
toLocalTimeZone,
49+
fromDate,
50+
fromAbsolute
51+
} from './conversion';
4252
export {
4353
isSameDay,
4454
isSameMonth,

0 commit comments

Comments
 (0)