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
// CommonJSvar TimeZoneDate = ;
// AMD;
// Script Tagvar TimeZoneDate = windowTimeZoneDate;
API
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:
// Assume the current machine is set to UTC-1var dateInUTCMinus1 = '1/1/2014 12:00:00';var dateInUTCPlus1 = 1 dateInUTCMinus1; dateInUTCMinus1 === dateInUTCPlus1;// true dateInUTCMinus1;// Wed Jan 01 2014 12:00:00 GMT-0100 dateInUTCPlus1;// Wed Jan 01 2014 14:00:00 GMT+0100
If a string is passed in, the TimeZoneDate will be set to that date/time. For example:
// Assume the current machine is set to UTC-1var dateInUTCMinus1 = '1/1/2014 12:00:00';var dateInUTCPlus1 = 1 '1/1/2014 12:00:00'; dateInUTCMinus1 === dateInUTCPlus1;// false dateInUTCMinus1;// Wed Jan 01 2013 12:00:00 GMT-0100 dateInUTCPlus1;// Wed Jan 01 2013 12:00:00 GMT+0100
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.