ECMAScript Dates are simply a time value that is an offset from 1970-01-01T00:00:00Z (the ECMAScript epoch, which is the same as the Java and UNIX epoch).
If a system's clock is accurately set to the current time, then Date.now()
and new Date().getTime()
for a particular instant will return exactly the same value regardless of the timezone offset of the host system. In practice there will however be minor variations due to clock inaccuracies and lack of syncrhonisation.
The host timezone offset comes from the host system and is only used for calculations involving local values, it's not an attribute of the Date itself.
If you want to get the time value for a particular date and time for a particular timezone offset, you can build a string that should be parsable by the built–in parser and use that. E.g. the Line Islands have an offset of +14:00 and don't observe daylight saving, so to get the time value for Christmas morning use a supported string format1 and the built–in parser:
// Christmas morning in Line Islands
let s = '2019-12-25T00:00:00+14:00';
// Get time value
let ms = Date.parse(s);
let d = new Date(ms);
let optsLocal = {weekday:'short', day:'numeric', month:'short', year:'numeric', hour:'numeric', hour12:false, minute:'numeric'};
let optsHK = Object.assign({},optsLocal,{timeZone:'Asia/Hong_Kong'});
let optsNY = Object.assign({},optsLocal,{timeZone:'America/New_York'});
let optsHW = Object.assign({},optsLocal,{timeZone:'Pacific/Honolulu'});
console.log('Start string: ' + s +
'\nTime value : ' + ms +
'\nLocal date : ' + d.toLocaleString('en-GB', optsLocal) +
'\nHong Kong : ' + d.toLocaleString('en-GB', optsHK) +
'\nNew York USA: ' + d.toLocaleString('en-GB', optsNY) +
'\nHawaii USA : ' + d.toLocaleString('en-GB', optsHW)
);
- Where "supported string format" is one of the two formats specified in ECMA-262, also see Why does Date.parse give incorrect results?