Skip to content
HungryCosmos edited this page Oct 28, 2017 · 2 revisions

This usage example assumes you have eztz package downloaded or installed.

// Import, if you are using npm, or just load it within <script> tags, as described in #installation
var eztz = require('eztz');


/************************************************************
 *  Get current time in various timezones, specifying it's  *
 *  UTC time as first argument                              *
 ************************************************************/

var timeUtc1 = eztz.get(0);                         // UTC
var timeUtc2 = eztz.get();                          // UTC done pro- way
var timeInAlmaty = eztz.get(+6);                    // Almaty (GMT+6)
var timeInMoscow = eztz.get(3);                     // Moscow (GMT+3), note that `+` is not necessary
var timeInSanFrancisco = eztz.get(-7);              // San Francisco (GMT-7)
var timeInIndia = eztz.get(5.5);                    // India (GMT+5:30). Offset of `5h 30m` is basically `5.5h`
var timeFictive = eztz.get(+25.852);                // Fictive Time Zone, say GMT+25.852, still works


/*************************************************************
 *  Time difference in hours between specified Date objects  *
 *************************************************************/

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

var diffDefault1 = eztz.diff(timeFictive, timeUtc1);     // 25.9   - 1 decimal place by default
var diffDefault2 = eztz.diff(timeFictive, timeUtc1, 1);  // 25.9   - 1 decimal place set explicitly
var diffRound = eztz.diff(timeFictive, timeUtc1, 0);     // 26     - 0 decimal places, chopped away
var diffExact = eztz.diff(timeFictive, timeUtc1, 3);     // 25.852 - 3 decimal places, exact value in this case
var diffUtcIndia = eztz.diff(timeUtc1, timeInIndia, 0);  // -5     - not -6, just as it's based on Math.round()


/*************************************************************
 *  Get date shifted against another Date                    *
 *************************************************************/

var timeGmt = eztz.get(-6, timeInAlmaty);                // GMT is 6h behind Almaty
var timeSanFrancisco = eztz.get(-13, timeInAlmaty);      // San Francisco is 13h behind Almaty
var timeInMoscow2 = eztz.get(3, timeGmt);                // Moscow is 3h ahead GMT; the same as eztz.get(3), line 13


/*************************************************************
 *  Value returned by this library is just a local Date      *
 *  shifted to match the required timezone                   *
 *************************************************************/

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


/*************************************************************
 *  Further manipulation may produce unexpected results,     *
 *  eg .getTime() should actually return the same value      *
 *************************************************************/

console.log(timeInMoscow.getTime());                     // 1509138546929
console.log(timeInAlmaty.getTime());                     // 1509149346929


/*************************************************************
 *  However, in practice it does the job.                    *
 *  See https://stackoverflow.com/a/11964609/8722066         *
 *************************************************************/

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 }));
Clone this wiki locally