Gathering detailed insights and metrics for @date-fns/tz
Gathering detailed insights and metrics for @date-fns/tz
Gathering detailed insights and metrics for @date-fns/tz
Gathering detailed insights and metrics for @date-fns/tz
npm install @date-fns/tz
99.6
Supply Chain
100
Quality
87.2
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
87 Stars
71 Commits
5 Forks
1 Watching
2 Branches
51 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (82.91%)
JavaScript (16.21%)
Makefile (0.88%)
Cumulative downloads
Total Downloads
Last day
5.8%
64,123
Compared to previous day
Last week
12.2%
346,686
Compared to previous week
Last month
79.5%
1,238,455
Compared to previous month
Last year
0%
2,035,749
Compared to previous year
19
The package provides Date
extensions TZDate
and TZDateMini
that perform all calculations in the given time zone rather than the system time zone.
Using it makes date-fns operate in given time zone but can be also used without it.
Like everything else in the date-fns ecosystem, the library is build-size aware. The smallest component, TZDateMini,
is only 916 B
.
Need only UTC? See @date-fns/utc that provides lighter solution.
1npm install @date-fns/tz --save
TZDate
and TZDateMini
have API similar to Date
, but perform all calculations in the given time zone, which might be essential when operating across different time zones, calculating dates for users in different regions, or rendering chart or calendar component:
1import { TZDate } from "@date-fns/tz"; 2import { addHours } from "date-fns"; 3 4// Given that the system time zone is America/Los_Angeles 5// where DST happens at Sunday, 13 March 2022, 02:00:00 6 7// Using system time zone will produce 03:00 instead of 02:00 because of DST: 8const date = new Date(2022, 2, 13); 9addHours(date, 2).toString(); 10//=> 'Sun Mar 13 2022 03:00:00 GMT-0700 (Pacific Daylight Time)' 11 12// Using Asia/Singapore will provide expected 02:00: 13const tzDate = new TZDate(2022, 2, 13, "Asia/Singapore"); 14addHours(tzDate, 2).toString(); 15//=> 'Sun Mar 13 2022 02:00:00 GMT+0800 (Singapore Standard Time)'
You can pass IANA time zone name ("Asia/Singapore", "America/New_York", etc.) or UTC offset ("+01:00", "-2359", or "+23"):
1new TZDate(2022, 2, 13, "Asia/Singapore"); 2 3new TZDate(2022, 2, 13, "+08:00"); 4 5new TZDate(2022, 2, 13, "-2359");
TZDate
and TZDateMini
The main difference between TZDate
and TZDateMini
is the build footprint. The TZDateMini
is 916 B
, and the TZDate
is 1.2 kB
. While the difference is slight it might be essential in some environments and use cases.
Unlike TZDateMini
which implements only getters, setters, and getTimezoneOffset
, TZDate
also provides formatter functions, mirroring all original Date
functionality:
1import { TZDateMini, TZDate } from "@date-fns/tz"; 2 3// TZDateMini will format date-time in the system time zone: 4new TZDateMini(2022, 2, 13).toString(); 5//=> 'Sat Mar 12 2022 16:00:00 GMT-0800 (Pacific Standard Time)' 6 7// TZDate will format date-time in the Singapore time zone, like expected: 8new TZDate(2022, 2, 13).toString(); 9//=> 'Sun Mar 13 2022 00:00:00 GMT+0800 (Singapore Standard Time)'
Even though TZDate
has a complete API, developers rarely use the formatter functions outside of debugging, so we recommend you pick the more lightweight TZDateMini
for internal use. However, in environments you don't control, i.e., when you expose the date from a library, using TZDate
will be a safer choice.
TZDate
All the TZDate
docs are also true for TZDateMini
.
When creating TZDate
, you can pass the time zone as the last argument:
1new TZDate(2022, 2, "Asia/Singapore");
2
3new TZDate(timestamp, "Asia/Singapore");
4
5new TZDate("2024-09-12T00:00:00Z", "Asia/Singapore");
The constructor mirrors the original Date
parameters except for the last time zone parameter.
TZDate.tz
The static tz
function allows to construct TZDate
instance with just a time zone:
1// Create now in Singapore time zone:
2TZDate.tz("Asia/Singapore");
3
4// ❌ This will not work, as TZDate expects a date string:
5new TZDate("Asia/Singapore");
6//=> Invalid Date
Just like the constructor, the function accepts all parameters variants:
1TZDate.tz("Asia/Singapore", 2022, 2);
2
3TZDate.tz("Asia/Singapore", timestamp);
4
5TZDate.tz("Asia/Singapore", "2024-09-12T00:00:00Z");
timeZone
The readonly timeZone
property returns the time zone name assigned to the instance:
1new TZDate(2022, 2, 13, "Asia/Singapore").timeZone; 2// "Asia/Singapore"
The property might be undefined
when created without a time zone:
1new TZDate().timeZone; 2// undefined
withTimeZone
The withTimeZone
method allows to create a new TZDate
instance with a different time zone:
1const sg = new TZDate(2022, 2, 13, "Asia/Singapore"); 2const ny = sg.withTimeZone("America/New_York"); 3 4sg.toString(); 5//=> 'Sun Mar 13 2022 00:00:00 GMT+0800 (Singapore Standard Time)' 6 7ny.toString(); 8//=> 'Sat Mar 12 2022 11:00:00 GMT-0500 (Eastern Standard Time)'
[Symbol.for("constructDateFrom")]
The TZDate
instance also exposes a method to construct a Date
instance in the same time zone:
1const sg = TZDate.tz("Asia/Singapore"); 2 3// Given that the system time zone is America/Los_Angeles 4 5const date = sg[Symbol.for("constructDateFrom")](new Date(2024, 0, 1)); 6 7date.toString(); 8//=> 'Mon Jan 01 2024 16:00:00 GMT+0800 (Singapore Standard Time)'
It's created for date-fns but can be used in any context. You can access it via Symbol.for("constructDateFrom")
or import it from the package:
1import { constructFromSymbol } from "@date-fns/tz";
tz
The tz
function allows to specify the context for the [date-fns] functions (starting from date-fns@4):
1import { isSameDay } from "date-fns"; 2import { tz } from "@date-fns/tz"; 3 4isSameDay("2024-09-09T23:00:00-04:00", "2024-09-10T10:00:00+08:00", { 5 in: tz("Europe/Prague"), 6}); 7//=> true
tzOffset
The tzOffset
function allows to get the time zone UTC offset in minutes from the given time zone and a date:
1import { tzOffset } from "@date-fns/tz"; 2 3const date = new Date("2020-01-15T00:00:00Z"); 4 5tzOffset("Asia/Singapore", date); 6//=> 480 7 8tzOffset("America/New_York", date); 9//=> -300 10 11// Summer time: 12tzOffset("America/New_York", "2020-01-15T00:00:00Z"); 13//=> -240
Unlike Date.prototype.getTimezoneOffset
, this function returns the value mirrored to the sign of the offset in the time zone. For Asia/Singapore (UTC+8), tzOffset
returns 480, while getTimezoneOffset
returns -480.
tzScan
The function scans the time zone for changes in the given interval. It returns an array of objects with the date of the change, the offset change, and the new offset:
1import { tzScan } from "@date-fns/tz"; 2 3tzScan("America/New_York", { 4 start: new Date("2020-01-01T00:00:00Z"), 5 end: new Date("2024-01-01T00:00:00Z"), 6}); 7//=> [ 8//=> { date: 2020-03-08T07:00:00.000Z, change: 60, offset: -240 }, 9//=> { date: 2020-11-01T06:00:00.000Z, change: -60, offset: -300 }, 10//=> { date: 2021-03-14T07:00:00.000Z, change: 60, offset: -240 }, 11//=> { date: 2021-11-07T06:00:00.000Z, change: -60, offset: -300 }, 12//=> { date: 2022-03-13T07:00:00.000Z, change: 60, offset: -240 }, 13//=> { date: 2022-11-06T06:00:00.000Z, change: -60, offset: -300 }, 14//=> { date: 2023-03-12T07:00:00.000Z, change: 60, offset: -240 }, 15//=> { date: 2023-11-05T06:00:00.000Z, change: -60, offset: -300 } 16//=> ]
See the changelog.
No vulnerabilities found.
No security vulnerabilities found.