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

pacific-time-offset

v1.0.0

Published

Get the U.S. Pacific Time offset for the current or given date.

Downloads

190

Readme

pacific-time-offset

Get the U.S. Pacific Time offset for the current or given date.

$ yarn add pacific-time-offset
$ npm install pacific-time-offset

It ONLY supports:

  • Pacific Time.
  • U.S. policy effective 2007 onwards.

This will not necessarily be correct for dates before 2007 or for territories where this policy does not apply.

Usage

The default export is a function that will return either the Pacific Standard Time or Pacific Daylight Time offset (from UTC) in minutes, either -480 or -420. Note that this is the reverse of what JavaScript’s weird Date getTimezoneOffset() method would return, which is the offset to UTC.

If an argument is supplied, it must be a Date instance.

import pacificTimeOffset from 'pacific-time-offset';

// 480 or 420, depending on current date.
pacificTimeOffset();
pacificTimeOffset(new Date());

// 480
pacificTimeOffset(new Date('2020-01-01T00:00:00.000Z'));
pacificTimeOffset(new Date(2020, 0, 1));
pacificTimeOffset(new Date(1579296618326));
pacificTimeOffset(new Date(2020, 0, 15));

// 420
pacificTimeOffset(new Date('2020-06-01T00:00:00.000Z'));
pacificTimeOffset(new Date(2020, 5, 1));
pacificTimeOffset(new Date(1594848671814));
pacificTimeOffset(new Date(2020, 6, 15));

Helpers

Available on the exported pacificTimeOffset function.

PST

The value -480. You could use this for expressions like pacificTimeOffset() === PST, for example.

PDT

The value -420. You could use this for expressions like pacificTimeOffset() === PDT, for example.

isDaylightTime

If you just want to know whether the given date is in Pacific Standard Time or Pacific Daylight Time and don’t care about the exact offset, there is also an isDaylightTime helper:

import { isDaylightTime } from 'pacific-time-offset';

// true
isDaylightTime(new Date('2020-06-01T00:00:00.000Z'));

// false
isDaylightTime(new Date('2020-01-01T00:00:00.000Z'));

Motivation

JavaScript dates are always in the system’s current time zone, so the only information available is the local time zone offset. It is very difficult to determine information about other time zones without pulling in a full library like Moment or Luxon which incorporate the full IANA time zone database (or something like it).

If you only care about U.S. Pacific Time policy and recent dates, this library will be much, much smaller (even if you trim down builds of the above libraries). This matters if you’re shipping the code in question to browsers.

Why would I use this?

Sometimes you have business functions that consider dates in a specific time zone, not necessarily the user’s local time zone. For instance, maybe you’re running an e-commerce site where timed messages, promotions, etc. are all based on the company’s “official current time” in a specific time zone. If you have applications running on user’s devices (like in their web browser), then how do you tell what time it is in that time zone? Well, if that time zone happens to be Pacific Time, you’re in luck – that’s what this does.

Alternatives

If you know you will only be running on platforms that support Intl.DateTimeFormat, have consistent locale/language support, and have data for the time zone you’re interested in (implementations technically must only support GMT), then you may be able to use that like so:

const formatter = new Intl.DateTimeFormat('en-US', {
  timeZone: 'America/Los_Angeles',
  timeZoneName: 'short'
});
const isDaylightTime = date => formatter.format(date).endsWith('PDT');

Again, support for this is implementation dependent. If you don’t want to worry about the platform’s locale, time zone, and Intl support, use this library.