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

eztz

v1.1.2

Published

I just want to get current date and time in a timezone using ONLY it's offset in hours. I'm so done looking for proper and 100% valid names.

Downloads

9

Readme

eztz

Build Status Coverage Status NPM version NPM downloads semantic-release Commitizen wannabee

I just want to get current date and time in a timezone using ONLY it's offset in hours. I'm so done looking for proper and 100% valid names.

About

This tool can do two things:

  1. get - time in any timezone as Date knowing only it's UTC time offset:
    • UTC time: eztz.get(0)
    • Almaty time (GMT+6): eztz.get(+6)
    • San Francisco time, knowing it's 13h behind Almaty: eztz.get(-13, timeInAlmaty)
  2. diff - calcualte time difference (measured in hours) as number between two Date objects:
    • Time difference between Almaty and UTC: eztz.diff(timeInAlmaty, timeUtc), which returns +6
    • Time difference between Moscow and Almaty: eztz.diff(timeInMoscow, timeInAlmaty), which returns -3

NOTE: value returned by this library is just a local date shifted to match the required timezone:

// Prints 6 for me, as my local UTC offset is +6.
console.log(timeInMoscow.getTimezoneOffset() / -60);

However, In practice it does the job

console.log(timeInSanFrancisco.toLocaleString());   // 2017-10-27 17:09:06
console.log(timeInAlmaty.toLocaleString());         // 2017-10-28 06:09:06
console.log(timeInMoscow.toLocaleString());         // 2017-10-28 03:09:06
console.log(timeInMoscow.toLocaleString('en-US', {  // Oct 28, 3:09 AM
  month: 'short',
  day: 'numeric',
  hour: 'numeric',
  minute:'numeric',
  hour12: true }));

More detailed review of common usecases can be found below, in Usage section
Some info on Working Environment

Installation

This package is distributed via npm:

npm install eztz

And available at npmcdn(https://unpkg.com/):

Browser-ready:

<script src="https://unpkg.com/[email protected]/dist/umd/eztz.min.js"></script>

Usage

Read step-by-step guide below, or copy-paste compiled sample in wiki

  1. Import

    var eztz = require('eztz');
  2. Get current time in various timezones, specifying it's UTC time offset as first argument
    2.1. UTC

    var timeUtc = eztz.get(0);

    2.2. Pro- way to get UTC date-time

    var timeUtc = eztz.get();

    2.3. Almaty (GMT+6)

    var timeInAlmaty = eztz.get(+6);

    2.4. Moscow (GMT+3), note that + is not necessary, since +3 is just 3

    var timeInMoscow = eztz.get(3);

    2.5. San Francisco (GMT-7)

    var timeInSanFrancisco = eztz.get(-7);

    2.6. India (GMT+5:30). Offset of 5h 30m is basically 5.5h

    var timeInIndia = eztz.get(5.5);

    2.7. Fictive Time Zone, say GMT+25.852, still works

    var timeFictive = eztz.get(+25.852);
  3. Time difference in hours between specified Date objects
    3.1. Calculated basically by substracting right from left

    var diffFromAlmatyToMoscow = eztz.diff(timeInAlmaty, timeInMoscow);  // +3

    3.2. Provides negated values if switch places

    var diffFromMoscowToAlmaty = eztz.diff(timeInMoscow, timeInAlmaty);  // -3

    3.3. Limits number of decimal places to 1 by default

    var diffDefault = eztz.diff(timeFictive, timeUtc);       // 25.9

    3.4. But you can specify it explicitly as third argument

    var diffDefault = eztz.diff(timeFictive, timeUtc, 1);    // 25.9

    3.5. And drop decimal part by feeding 0

    var diffRound = eztz.diff(timeFictive, timeUtc, 0);      // 26

    3.5. Or get exact value with 3

    var diffExact = eztz.diff(timeFictive, timeUtc, 3);      // 25.852

    3.6. Implemented with Math.round()

    var diffUtcIndia = eztz.diff(timeUtc, timeInIndia, 0);   // -5, not -6
  4. Get date shifted against another Date
    4.1. GMT is 6h behind Almaty

    var timeGmt = eztz.get(-6, timeInAlmaty);

    4.2. San Francisco is 13h behind Almaty

    var timeSanFrancisco = eztz.get(-13, timeInAlmaty);

    4.3. Moscow is 3h ahead GMT

    var timeInMoscow = eztz.get(3, timeGmt);    // The same as eztz.get(3)
  5. NOTE
    5.1. Value returned by this library is just a local date shifted to match the required timezone

    // Prints 6 for me, as my local UTC offset is +6.
    console.log(timeInMoscow.getTimezoneOffset() / -60);

    5.2. Further manipulation may produce unexpected results, for example .getTime() should actually return the same value

    console.log(timeInMoscow.getTime());                // 1509138546929
    console.log(timeInAlmaty.getTime());                // 1509149346929
  6. However
    6.1. In practice it does the job

    console.log(timeInSanFrancisco.toLocaleString());   // 2017-10-27 17:09:06
    console.log(timeInAlmaty.toLocaleString());         // 2017-10-28 06:09:06
    console.log(timeInMoscow.toLocaleString());         // 2017-10-28 03:09:06
    console.log(timeInMoscow.toLocaleString('en-US', {  // Oct 28, 3:09 AM
        month: 'short',
        day: 'numeric',
        hour: 'numeric',
        minute:'numeric',
        hour12: true }));

Click to see full compiled usage example in wiki page

Working Environment

Prerequisites:

References

This library was developed by me to power up my github page
Thanks to Kent C. Dodds and his series on starwars-names