1

I need a time zone offset (8, in the case of Los Angeles from existing code within which I am making modifications, relative from a specific reference location) to compute a new time for that time zone.

What do I need to modify in console command #2 to get the Time Zone offset, inside a specific Time Zone, in my case, the return value of the invoked function inside Command #1?

Console Command 1 Outputs PST

 function getTimezoneName() {
    const today = new Date();
    const short = today.toLocaleDateString(undefined);
    const full = today.toLocaleDateString(undefined, { timeZoneName: 'short' });

    // Trying to remove date from the string in a locale-agnostic way
    const shortIndex = full.indexOf(short);
    if (shortIndex >= 0) {
        const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
                    
        // by this time `trimmed` should be the timezone's name with some punctuation -
        // trim it from both sides
        return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');
    } else {
        // in some magic case when short representation of date is not present in the long one, just 
        // return the long one as a fallback, since it should contain the timezone's name
        return full;
    }
}
getTimezoneName();

Console Command 2 Outputs 0 -- not desired

Written, with modifications, from https://stackoverflow.com/a/57842203/1691103

  let date = Date.UTC(2021, 1, 5, 4, 13, 0, 0);
  let utc = parseInt((date.toLocaleString('en-US', { timeZone: "UTC" })).replaceAll(",", ""));
  let est = parseInt((date.toLocaleString('en-US', { timeZone: "America/Los_Angeles" })).replaceAll(",", ""));
  let utcDate = new Date(utc);
  let tzDate = new Date(est);
  let offsetVal = utcDate.getTime() - tzDate.getTime();
  console.log(offsetVal);  <-------outputs 0 to console
Vahe
  • 1,699
  • 3
  • 25
  • 76

0 Answers0