Moment.js gives Invalid date in Firefox

后端 未结 2 1768
悲哀的现实
悲哀的现实 2020-11-28 15:27

I have a requirement to convert date time in moment.js. But it gives me different result in Chrome and Firefox.

In Google Chrome it gives correct result but in Mozi

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 15:37

    You're not specifying a format for parsing the string 2016-Jan-02. So moment falls back to the native Date object, which is inconsistent across the different browsers. To parse the date consistently, include a format string along with it.

    e.g.

    moment("2016-Jan-02", "DD-MMM-YYYY")
    

    Then if you want to format the moment object as a string, you can do what you were doing before:

    moment("2016-Jan-02", "DD-MMM-YYYY").format("DD-MM-YYYY")
    

    which returns the string 02-01-2016 in both browsers.

提交回复
热议问题