Question

for a project of mine, I need to have a solution for showing the exact (date and) time for a couple of defined cities (London, New York, Hong Kong) together with the local time - no matter where in the world I execute that JavaScript code. I need to work in JavaScript without internet connection (offline HTML).

var myowntime = new Date();
var myhours = myowntime.getHours();
var myminutes = myowntime.getMinutes();
var utch = myowntime.getUTCHours();
var utcm = myowntime.getUTCMinutes();
var localdst;

var london_hours, london_minutes;
var ny_hours, ny_minutes;
var hk_hours, hk_minutes;

// Local Daylight Saving and TimeZone calculation

// local DST first
var jan = new Date(myowntime.getFullYear(), 0, 1);
var jul = new Date(myowntime.getFullYear(), 6, 1);
if (myowntime.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset())) {
  localdst = true;
} else {
  localdst = false;
}
var dst_hours = 0;
if (localdst) {
  dst_hours = (Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset() - myowntime.getTimezoneOffset())/60;
}
mydiff_utc_hours = -myowntime.getTimezoneOffset()/60;
if (localdst) {
  mydiff_utc_hours -= dst_hours;
}

london_hours = utch;
if (localdst) {
  london_hours += dst_hours;
}
ny_hours = utch - 5;
if (localdst) {
  ny_hours += dst_hours;
}
hk_hours = utch + 8; // no DST as far as I know

if (hk_hours > 23) {
  hk_hours -= 24;
}
if (hk_hours < 0) {
  hk_hours += 24;
}

if (ny_hours > 23) {
  ny_hours -= 24;
}
if (ny_hours < 0) {
  ny_hours += 24;
}

if (london_hours > 23) {
  london_hours -= 24;
}
if (london_hours < 0) {
  london_hours += 24;
}

Problem... I can only detect my local DST and I assume that it's +60 minutes. But I use it for London and NY time, too (which is a wrong assumption).

How do I calculate other timezones' DST hour value and the dates when DST applies in these time zones. I'd be most happy to see a working example of JS code for this...

P.S.: I am working only with MS Windows environment. These OS holds all the time zone DST info somewhere in its depths... is it possible to collect this info via JS or VBScript from there somehow (WMI?)?

Thank you in advance,

Oliver

Was it helpful?

Solution

Maybe check out this library:

http://momentjs.com/timezone/

Looks like it natively gives you what you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top