re: Get current time from specified timezone in JavaScript? VIEW POST


It appears moment.js is broken, it always provides the error

moment.js:22 Uncaught TypeError: Cannot read property 'version' of undefined
    at moment.js:22
    at moment.js:16
    at moment.js:18

with these lines of code,:

// Resolves es6 module loading issue
if (moment.version === undefined && moment.default) {
    moment = moment.default;

It works just fine. As per the docs, you need moment, moment-timezone, and some timezone data. In the example below I downloaded both files from moment's website.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <div id="test"></div>
    <script src="moment.min.js"></script>
    <script src="moment-timezone-with-data.js"></script>
      const newYork = moment.tz("2014-06-01 12:00", "America/New_York");
      const losAngeles = newYork.clone().tz("America/Los_Angeles");
      const london = newYork.clone().tz("Europe/London");

      document.querySelector("#test").innerHTML = `
        <p>newYork.format(): ${newYork.format()}</p>
        <p>losAngeles.format(): ${losAngeles.format()}</p>
        <p>london.format(): ${london.format()}</p>
code of conduct - report abuse