Javascript date differs based on month format

This question already has an answer here:

  • Why does Date.parse give incorrect results? 10 answers

  • As indicated on MDN, using the Date constructor to parse dates is discouraged because of implementation differences.

    However, if you really want to do so, the only solution is to ensure that the inputs you provide are consistent. This will ensure that (at least within a single environment) the outputs will be consistent.

    To have both work like 2017-5-19 :

    function adjustDateString(str) {
        return str.split('-').map(Number).join('-');
    }
    
    console.log(new Date(adjustDateString('2017-5-19')))
    console.log(new Date(adjustDateString('2017-05-19')))
    链接地址: http://www.djcxy.com/p/18632.html

    上一篇: 使用JavaScript在新标签(而不是新窗口)中打开一个URL

    下一篇: Javascript日期根据月份格式而有所不同