how to fix Javascript Invalid Date Error in FF and IE

This question already has an answer here:

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

  • The only formats officially supported by the Date() constructor (which calls Date.parse(...) ) are IETF-compliant RFC 2822 timestamps and ISO8601.

    Any other formats are implementation specific and may not be supported cross-browser.

    A quick dependency-free way to create a date would be to parse it yourself. For example, using regexes:

    function parseDate(date) {
      var MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', ...];
      var date = '11-May-16';
      var match = date.match(/(d{2})-([A-Za-z]{3})-(d{2})/);
      return new Date(2000 + parseInt(match[3]), MONTHS.indexOf(match[2]), match[1]);
    }
    
    parseDate('11-May-16')
    -> Wed May 11 2016 00:00:00
    
    链接地址: http://www.djcxy.com/p/18626.html

    上一篇: Date.parse不起作用

    下一篇: 如何修复FF和IE中的无效日期错误