How does the Date() in javascript work?

前端 未结 1 865
天命终不由人
天命终不由人 2020-12-22 15:32

I am trying to parse \"NINE/MAR/2008\" using Date function in JS as:

new Date(\"NINE/MAR/2008\")

This is giving me result as:

相关标签:
1条回答
  • 2020-12-22 16:12

    The only formats that the Date constructor and Date.parse are required by the specification to support are:

    1. The one introduced by ECMAScript 5th edition in 2009 such as 2015-04-29, which was meant to be a simplified version of ISO-8601 but they messed up on timezones (fixed in ECMAScript 2015, but then had to be amended again in ECMAScript 2016), and

    2. Whatever Date#toString outputs on that JavaScript engine (which is not specified).

    Most JavaScript engines will also parse mm/dd/yyyy (where those are all numbers — always in U.S. format, month-first), but that is not specified behavior. Some may parse yyyy/mm/dd (note the / rather than -), but again it's undefined behavior, and in particular some engines may interpret it as UTC and others as local time (and it matters, even if it's just a date without a time on it).

    All other date parsing has to be done by your own code or code in a library that you use, such as MomentJS. You can build a Date from its constituent parts using the long form of the constructor, e.g.: new Date(2008, 2, 9) for 9th March, 2008 (note that months start with 0 = January).

    0 讨论(0)
提交回复
热议问题