@date-fns/utc
The package provides Date
extensions UTCDate
and UTCDateMini
that perform
all calculations in UTC rather than the system time zone.
Using it makes [date-fns] operate in UTC but can be also used without it.
Installation
npm install @date-fns/utc --save
Usage
UTCDate
and UTCDateMini
have API identical to Date
, 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";
const date = new Date(2022, 2, 13);
addHours(date, 2).toString();
const utcDate = new UTCDate(2022, 2, 13);
addHours(utcDate, 2).toString();
Difference between UTCDate
and UTCDateMini
Unlike UTCDateMini
which implements only getters, setters, and
getTimezoneOffset
, UTCDate
also provides formatter functions, mirroring
all original Date
functionality:
import { UTCDateMini, UTCDate } from "@date-fns/utc";
new UTCDateMini(2022, 2, 13).toString();
new UTCDate(2022, 2, 13).toString();
Even though UTCDate
has a complete API, developers rarely use the formatter
functions outside of debugging, so we recommend you pick the more lightweight
UTCDateMini
for internal use. However, in environments you don't control,
i.e., when you expose the date from a library, using UTCDate
will be
a safer choice.
For instance, in date-fns locale functions (format
, parse
, etc.), we use
UTCDateMini
to ensure the minimal build size.
Changelog
See the changelog.
License
MIT © Sasha Koss