date() function returning Invalid Date in Safari and Firefox

前端 未结 5 1375
清酒与你
清酒与你 2021-02-14 03:57

I am formatting a date in the following way:

date = new Date(\"2013-05-12 20:00:00\");
formattedDate = new Date(date.getFullYear(),date.getMonth(),date.getDate()         


        
5条回答
  •  孤独总比滥情好
    2021-02-14 04:49

    I think you need a 'T' between the date and the time to get it working fully.

    date = new Date("2013-05-12T20:00:00");
    

    There are quite a few possible datetime formats allowed in the spec and I don't think all browsers offer all possibilities. For Firefox see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse

    To be really safe cross-browser you might want to try the RFC2822 / IETF date syntax (basically the same as Chrome spat back out at you)

提交回复
热议问题