Search code examples
javascriptdateinternet-explorer-11momentjsdatejs

In Internet Explorer not able to convert UTC date to local date


Here is date that I received from Backend "2017-05-23T18:30:00",

This works fine in Chrome:

  • Call: new Date('2017-05-23T18:30:00').toString()
  • Result: "Wed May 24 2017 00:00:00 GMT+0530 (India Standard Time)"

But on the Internet Explorer:

  • Call: new Date('2017-05-23T18:30:00').toString()
  • Result: "Tue May 23 2017 18:30:00 GMT+0530 (India Standard Time)",

What to do get local date time from UTC date in Internet Explorer as I am getting in Chrome?


Solution

  • You can use moment.utc to parse your input cross browser. Then you can use format() to display your moment object. If you need to convert moment object to JavaScript date you can use toDate() method.

    Use local() if you want to convert your moment to local time.

    See Local vs UTC vs Offset for additional information.

    Here a live sample:

    var input = '2017-05-23T18:30:00';
    var m = moment.utc(input);
    console.log(m.format());
    console.log(m.toDate().toString());
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>