~ 2 min read

Javascript - Date and Time for any TimeZone

A little something I wrote since I did not find anything that does that readily available. It looks solid but may contain bugs for corner cases or possibly other minor inconsistencies. For my needed timezones it seems to work fine.

/**
 * Returns a Date object with the time setup for the desired remote timezone
 * e.g.
 * EST   - UTC/GMT-5 --> getGMTRelativeDate(-5);
 * EDT   - UTC/GMT-4 --> getGMTRelativeDate(-4);
 * PST   - UTC/GMT-8 --> getGMTRelativeDate(-8);
 * PDT   - UTC/GMT-7 --> getGMTRelativeDate(-7);
 * Rome  - UTC/GMT+2 --> getGMTRelativeDate(2);
 * Japan - UTC/GMT+9 --> getGMTRelativeDate(9);
 * etc...
 */
function function getGMTRelativeDate(GMTOffset) {
    var localDate = new Date();
    offset = localDate.getTimezoneOffset();
    LOCALOffset = (offset / -60)

    tzDiff = LOCALOffset - GMTOffset;
    localTime = new Date().getTime();

    RemoteTime = localTime - (tzDiff * 3600 * 1000);

    remoteDate = new Date(RemoteTime);
    //log("Remote time = " + remoteDate.toLocaleString());
    return remoteDate;
}

// To invoke just pass the desired diff with GMT
getGMTRelativeDate(-2);