Search code examples
javascript

Why new Date doesn't work in Firefox browser


I want to check difference between given date and now. I want to use exactly the code below and it works fine in chrome. but the console log says something else in Firefox.

Why? and how to fix this?

// input your custom Date below
const date = new Date('2020-6-23 14:22:00 GMT+0430'); // Set End Time Here!
const dif = (date.getTime() / 1000) - ((new Date().getTime())/1000);
const end = Math.max(0, dif);

console.log(date, dif, end);


Solution

  • "GMT" does not belong in that string. The string should be ISO 8601 compliant, if you want to be sure that it gets parsed correctly.

    To align it with the ISO standard:

    • remove "GMT" and the space before it,
    • separate the time part from the date part with a "T",
    • write the month with double digits.
    • Either use punctuation (hyphen, colon) in the date, time and timezone, or don't use any at all. This condition is not really clear in the ISO 8601 specs I have access to, but apparently there is a move towards a more strict definition which excludes using punctuation only in a part of the string. In short, you are sure to align when you add also a colon in +04:30.

    const date = new Date('2020-06-23T14:22:00+04:30');
    
    // Now also output it in the Iranian timezone:
    console.log(date.toLocaleString("en", { timeZone: "Iran" } ));

    BTW: GMT is an ambiguous term anyway. UTC is a more exact defined term.