TimeZoneDate
A JavaScript Date alternative that is specific to a given timezone other than the current machine's timezone.
Installation
npm install timezonedate
Environment Support
TimeZoneDate has been tested in Node, IE9+, Chrome, Firefox, and Opera.
Usage
var TimeZoneDate = require('timezonedate');
require(['timezonedate'], function(TimeZoneDate) { ... });
var TimeZoneDate = window.TimeZoneDate;
API
function TimeZoneDate(offset[, initialDate])
offset
type: Number
There are two ways to specify a timezone offset.
The easiest is by passing in the hours relative to UTC. So if you want to specify the timezone one hour behind UTC, you would pass in -1. For one hour ahead of UTC, you would pass in 1.
The other way to specify the timezone is to pass in what the result of new Date().getTimezoneOffset()
would be if it were run in the target timezone. For example, if your system clock is set to UTC-1, new Date().getTimezoneOffset()
will be 60. If you are in UTC+1, the result will be -60. Check out the MDN getTimezoneOffset documentation for more info.
initialDate
type: Date
or String
If a Date object is passed in, the TimeZoneDate will be set to the same moment, rather than the same time. For example:
var dateInUTCMinus1 = new Date('1/1/2014 12:00:00');
var dateInUTCPlus1 = new TimeZoneDate(1, dateInUTCMinus1);
dateInUTCMinus1.valueOf() === dateInUTCPlus1.valueOf();
dateInUTCMinus1.toString();
dateInUTCPlus1.toString();
If a string is passed in, the TimeZoneDate will be set to that date/time. For example:
var dateInUTCMinus1 = new Date('1/1/2014 12:00:00');
var dateInUTCPlus1 = new TimeZoneDate(1, '1/1/2014 12:00:00');
dateInUTCMinus1.valueOf() === dateInUTCPlus1.valueOf();
dateInUTCMinus1.toString();
dateInUTCPlus1.toString();
If the initialDate
parameter is omitted, it is the equivalent of passing in new Date()
.
The rest of the API is exactly the same as the native Date object.