How to parse a date in format “YYYYmmdd” in JavaScript?

后端 未结 5 1286
自闭症患者
自闭症患者 2020-11-27 21:55

This is a noob question:

How to parse a date in format \"YYYYmmdd\" without external libraries ? If the input string is not in this format I would like

5条回答
  •  萌比男神i
    2020-11-27 22:43

    simplistic answer maybe, without checks, but fast...

    var date = parseInt(date);
    new Date(date / 10000, date % 10000 / 100, date % 100);
    

    or, if months are not zero based in the source,

    new Date(date / 10000, (date % 10000 / 100) - 1, date % 100);
    

提交回复
热议问题