Date constructor: numeric arguments vs. string argument giving different dates in some cases

后端 未结 5 1943
孤街浪徒
孤街浪徒 2021-01-01 16:18

First of all, I think timezone probably has something to do with this. I\'m in EST/EDT. Also, I\'m testing this on chromium 17 / linux.

Now, let\'s say I create two

5条回答
  •  孤街浪徒
    2021-01-01 16:20

    Using "-" as a date separator for USA dates confuses some browsers, some will perform date arithmetic others will return NaN, so use the "/" date separator. A culture aware solution is to use date.js, which is an outstanding JavaScript date handler that resolves issues like the one you have pointed out (http://www.datejs.com/). Using the parse method removes all confusion:

    Date.parse("2020-12-15").toString() // yields the correct date ("Tue Dec 15 00:00:00 PST 2020"). 
    

提交回复
热议问题