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

tiny-timezone-offset

v1.0.2

Published

Simple tiny timezone offset utility function using Intl. See also get-timezone-offset

Downloads

3

Readme

tiny-timezone-offset

Get the timezone offset from UTC to desired timezone in js. Tiny (~500 Bytes) and no dependencies[^1]

See also get-timezone-offset by mobz (not affiliated).

Usage

Default

    import { getTimezoneOffset } from 'tiny-timezone-offset';
    const now = new Date(); 
    // Should be the date you wish to check the offset for. If this date is during DST, it will respect it.
    const { houroffset, minuteOffset, totalOffset } = getTimezoneOffset(now, "Europe/Amsterdam");
    // During daylight savings time, this should return { -2, 0, -120 }

This is in line with builtin Date.prototype.getTimezoneOffset() which gives you the timezone offset in 'minutes needed to add to return to UTC.' Date.prototype.getTimezoneOffset() does this for the machine's local timezone only.

Using the relative offset based on ISO 8601/ANSI standards

    import { getTimezoneOffset } from 'tiny-timezone-offset/ANSI';
    const now = new Date(); 
    // Should be the date you wish to check the offset for. If this date is during DST, it will respect it.
    const { houroffset, minuteOffset, totalOffset } = getTimezoneOffset(now, "Europe/Amsterdam");
    // During daylight savings time, this should return { 2, 0, 120 }

This returns the timezone offset in 'minutes past UTC'.

Differences

With the default method, one should ADD the offset to a UTC date to get the timezoned date in UTC.

With the ANSI method, one should SUBTRACT the offset from a UTC date to get the timezoned date in UTC.

i.e. a time of 10:00 in a timezone 2 hours ahead of UTC will be 08:00 in UTC.

If the offset given is -120 mins (like the default method): 10:00 + (-120 minutes) = 08:00. if the offset given is 120 mins (like the ANSI method) 10:00 - (120 minutes) = 08:00.

Troubleshooting

[^1]: If you are running getTimezoneOffset on the server and using alpine or other ultra minimal docker image without a timezone database, you need to install the tzdata package. Most docker images, including the "slim" images published by node already include tzdata.

Results for locations that experience daylight saving change throughout the year Results for locations that have moved timezone may vary as the tz database is modified. This sometimes includes retrospective changes.

How it works

getTimezoneOffset works by using the global Intl object, using the tz database of the underlying operating system. By formatting a date to a string a certain way with a given desired timezone name, it can find the timezone offset in the string and return it numerically.

Internationalization was introduced in 2012; Engine support is tracked here, but boils down to:

  • Node: v4+
  • Bun, Deno and other javascript runtimes
  • All browsers

Why use this over get-timezone-offset ?

  • Flexibility
    • Being able to get only the hour or only the minute offset can reduce complexity.
  • ANSI version available
    • If your code is more logical or more readable with a positive output for timezones ahead of UTC, this package might help.
  • Code readability
    • In my personal opinion, the source code of get-timezone-offset is not very clear to read.
  • File size
    • Both repositories are already incredibly small, but tiny-timezone-offset is almost 400 Bytes smaller!
  • Written for ESM
    • ESM is intended to replace CommonJS in the js ecosystem. This package is fully written in ESM and ready to be used with it

When NOT to use this over get-timezone-offset ?

  • Backwards compatibility
    • get-timezone-offset was written and tested to work in any browser and node versions of 4 and above. I have not written tiny-timezone-offset with that in mind. Please check if get-timezone-offset can fulfill your needs if backwards compat is a big deal to your project.
  • Performance
    • In a majority of my own benchmarks, get-timezone-offset was about ~10% faster than tiny-timezone-offset. I could possibly improve performance by using regular expressions, but as it stands, I use normal string operations. Please run the benchmark yourself to compare and pick what's best for you if performance matters.