2

I am trying to get the date/time and my code (below) works fine. unfortunately, on earliers version of chrome, it is unable to detect timezone and gives error -

RangeError: Invalid time zone specified: undefined
    at new DateTimeFormat (native)
    at Date.toLocaleString (native)

. So I need help on forcing it to UTC when no run-time timezone is detected. could anyone please assist?

Here is my code:

 const printedDate = new Date;
    const printedDateStringFormatted = printedDate.toLocaleString();

Please let me know if I am not clear enough to state my problem.

Dwayne
  • 151
  • 1
  • 11

2 Answers2

0
var aestTime = new Date().toLocaleString("en-US", {timeZone: "Australia/Brisbane"});
aestTime = new Date(aestTime);
console.log('AEST time: '+aestTime.toLocaleString())

var asiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Shanghai"});
asiaTime = new Date(asiaTime);
console.log('Asia time: '+asiaTime.toLocaleString())

var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
usaTime = new Date(usaTime);
console.log('USA time: '+usaTime.toLocaleString())

var indiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});
indiaTime = new Date(indiaTime);
console.log('India time: '+indiaTime.toLocaleString())

This is the standard way to work with localizations.

And them just do a loop like:

if(!timezone)
{
   timezone = utcTime;
}

Here nice docs about it

code from @Udhaya in this link.

Also this is nice:

/** 
 * function to calculate local time
 * in a different city
 * given the city's UTC offset
 */
function calcTime(city, offset) {

    // create Date object for current location
    var d = new Date();

    // convert to msec
    // add local time zone offset
    // get UTC time in msec
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    var nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();
}
Qiqke
  • 486
  • 5
  • 19
0

You can catch the error if you are unable to detect the local timezone, and in that case use UTC.

const printedDate = new Date;
let printedDateStringFormatted;

try {
  printedDateStringFormatted = printedDate.toLocaleString();
} catch (e) {
  const options = { timeZone: 'UTC', timeZoneName: 'short' };
  printedDateStringFormatted = printedDate.toLocaleString(undefined, options);
}

// Local datetime if available, otherwise will be datetime in UTC
console.log(printedDateStringFormatted);
Jonathan Irvin
  • 896
  • 8
  • 17
  • could you please explain the `printedDateStringFormatted = printedDate.toLocaleString(undefined, options);` – Dwayne Nov 18 '19 at 16:08
  • The undefined argument is for locales. It is where you would add the language tag, such as `en-US`. If the argument is undefined, this method returns localized digits specified by the OS, so basically you don't need to include it here, which is why I set it to undefined. With options, we are specifying that the timezone we want is UTC. – Jonathan Irvin Nov 18 '19 at 16:11
  • Got it . thanks . have one more doubt. I have below change which I haven't tested since I cannot reproduce it on my box. But my question is will below change fix my current issue which `RangeError: Invalid time zone specified: undefined at new DateTimeFormat (native) at Date.toLocaleString (native)` **Here was my change:** `let printedDateStringFormatted: string; try{ printedDateStringFormatted = printedDate.toString(); } catch{ printedDateStringFormatted = printedDate.toUTCString(); }` – Dwayne Nov 18 '19 at 16:45
  • Perfect Thanks. I will test your code too . If that works, will mark it as an answer – Dwayne Nov 18 '19 at 16:55