npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@date-fns/utc

v2.1.0

Published

UTC date utils

Downloads

552,568

Readme

@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.

Like everything else in the date-fns ecosystem, the library is build-size aware. The smallest component, UTCDateMini, is only 239 B.

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

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";

// 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:
const date = new Date(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:
const utcDate = new UTCDate(2022, 2, 13);
addHours(utcDate, 2).toString();
//=> 'Sun Mar 13 2022 02:00:00 GMT+0000 (Coordinated Universal Time)'

Difference between UTCDate and UTCDateMini

The main difference between UTCDate and UTCDateMini is the build footprint. The UTCDateMini is 239 B, and the UTCDate is 504 B. While the difference is slight, and 504 B is not significant by any means, it might be essential in some environments and use cases.

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";

// UTCDateMini will format date-time in the system time zone:
new UTCDateMini(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:
new UTCDate(2022, 2, 13).toString();
//=> 'Sun Mar 13 2022 00:00:00 GMT+0000 (Coordinated Universal Time)'

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.

API

UTCDate

UTCDate mirrors all the Date API, so refer to the MDN documentation for the full list of methods and properties.

utc

The utc function allows to specify the context for the date-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

Changelog

See the changelog.

License

MIT © Sasha Koss