Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
forked fromdate-fns/utc

date-fns UTC utils

License

NotificationsYou must be signed in to change notification settings

fabon-f/utc

 
 

Repository files navigation

The package providesDate extensionsUTCDate andUTCDateMini that performall calculations in UTC rather than the system time zone.

Using it makesdate-fns operate in UTC but can be also used without it.

Need more than just UTC? See@date-fns/tz that provides full time zone support.

Installation

npm install @date-fns/utc --save

Usage

UTCDate andUTCDateMini have API identical toDate, but perform all calculations in UTC, which might be essential when calculating abstract date-time, i.e for rendering chart or calendar component:

import{UTCDate}from"@date-fns/utc";import{addHours}from"date-fns";// Given that the system time zone is America/Los_Angeles// where DST happens at Sunday, 13 March 2022, 02:00:00// Using system time zone will produce 03:00 instead of 02:00 because of DST:constdate=newDate(2022,2,13);addHours(date,2).toString();//=> 'Sun Mar 13 2022 03:00:00 GMT-0700 (Pacific Daylight Time)'// Using UTC will provide expected 02:00:constutcDate=newUTCDate(2022,2,13);addHours(utcDate,2).toString();//=> 'Sun Mar 13 2022 02:00:00 GMT+0000 (Coordinated Universal Time)'

The package also provides autc function that allows to specify the context for thedate-fns functions (starting from date-fns@4):

import{isSameDay}from"date-fns";import{utc}from"@date-fns/utc";isSameDay("2024-09-09T23:00:00-04:00","2024-09-10T10:00:00+08:00",{in:utc,});//=> true

Difference betweenUTCDate andUTCDateMini

UnlikeUTCDateMini which implements only getters, setters, andgetTimezoneOffset,UTCDate also provides formatter functions, mirroringall originalDate functionality:

import{UTCDateMini,UTCDate}from"@date-fns/utc";// UTCDateMini will format date-time in the system time zone:newUTCDateMini(2022,2,13).toString();//=> 'Sat Mar 12 2022 16:00:00 GMT-0800 (Pacific Standard Time)'// UTCDate will format date-time in the UTC, like expected:newUTCDate(2022,2,13).toString();//=> 'Sun Mar 13 2022 00:00:00 GMT+0000 (Coordinated Universal Time)'

Even thoughUTCDate has a complete API, developers rarely use the formatterfunctions outside of debugging, so we recommend you pick the more lightweightUTCDateMini for internal use. However, in environments you don't control,i.e., when you expose the date from a library, usingUTCDate will bea safer choice.

Changelog

Seethe changelog.

License

MIT © Sasha Koss

About

date-fns UTC utils

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript59.7%
  • JavaScript33.1%
  • Makefile7.2%

[8]ページ先頭

©2009-2025 Movatter.jp