eztz
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:
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)
- UTC time:
diff
- calcualte time difference (measured in hours) asnumber
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
- Time difference between Almaty and UTC:
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;
However, In practice it does the job
console; // 2017-10-27 17:09:06console; // 2017-10-28 06:09:06console; // 2017-10-28 03:09:06console;
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:
Usage
Read step-by-step guide below, or copy-paste compiled sample in wiki
-
Import
var eztz = ; -
Get current time in various timezones, specifying it's UTC time offset as first argument
2.1. UTCvar timeUtc = eztz;2.2. Pro- way to get UTC date-time
var timeUtc = eztz;2.3. Almaty (GMT+6)
var timeInAlmaty = eztz;2.4. Moscow (GMT+3), note that
+
is not necessary, since+3
is just3
var timeInMoscow = eztz;2.5. San Francisco (GMT-7)
var timeInSanFrancisco = eztz;2.6. India (GMT+5:30). Offset of
5h 30m
is basically5.5h
var timeInIndia = eztz;2.7. Fictive Time Zone, say GMT+25.852, still works
var timeFictive = eztz; -
Time difference in hours between specified Date objects
3.1. Calculated basically by substracting right from leftvar diffFromAlmatyToMoscow = eztz; // +33.2. Provides negated values if switch places
var diffFromMoscowToAlmaty = eztz; // -33.3. Limits number of decimal places to 1 by default
var diffDefault = eztz; // 25.93.4. But you can specify it explicitly as third argument
var diffDefault = eztz; // 25.93.5. And drop decimal part by feeding 0
var diffRound = eztz; // 263.5. Or get exact value with 3
var diffExact = eztz; // 25.8523.6. Implemented with Math.round()
var diffUtcIndia = eztz; // -5, not -6 -
Get date shifted against another Date
4.1. GMT is 6h behind Almatyvar timeGmt = eztz;4.2. San Francisco is 13h behind Almaty
var timeSanFrancisco = eztz;4.3. Moscow is 3h ahead GMT
var timeInMoscow = eztz; // The same as eztz.get(3) -
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;5.2. Further manipulation may produce unexpected results, for example .getTime() should actually return the same value
console; // 1509138546929console; // 1509149346929 -
However
6.1. In practice it does the jobconsole; // 2017-10-27 17:09:06console; // 2017-10-28 06:09:06console; // 2017-10-28 03:09:06console;
Click to see full compiled usage example in wiki page
Working Environment
Prerequisites:
- NodeJS >= 4 to build. Current latest version is
8.8.1
, LTS is6.11.5
- NodeJS >= 8 to use auto-publishing with semantic-release
- npm, which usually comes packaged with Node
- git command line tool
- github, travis, coveralls accounts
- npm account
References
This library was developed by me to power up my github page
Thanks to Kent C. Dodds and his series on starwars-names