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

后端 未结 5 1271
自闭症患者
自闭症患者 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条回答
  • 2020-11-27 22:34

    A more robust version validating the numbers :

     function parse (str) {
            // validate year as 4 digits, month as 01-12, and day as 01-31 
            if ((str = str.match (/^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])$/))) {
               // make a date
               str[0] = new Date (+str[1], +str[2] - 1, +str[3]);
               // check if month stayed the same (ie that day number is valid)
               if (str[0].getMonth () === +str[2] - 1)
                  return str[0];
            }
            return undefined;
     }
    

    See fiddle at : http://jsfiddle.net/jstoolsmith/zJ7dM/

    I recently wrote a much more capable version you can find here : http://jsfiddle.net/jstoolsmith/Db3JM/

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-27 22:43

    Example using only the digits in the string:

    function toDate(str) {
      var m = str.split(/\D/);
      return new Date(+m[0], +m[1] - 1, +m[2], +m[3], +m[4], +m[5]);
    }
        
    console.log(toDate("2020-08-23 23:34:45"));

    0 讨论(0)
  • 2020-11-27 22:46
    function parse(str) {
        if(!/^(\d){8}$/.test(str)) return "invalid date";
        var y = str.substr(0,4),
            m = str.substr(4,2),
            d = str.substr(6,2);
        return new Date(y,m,d);
    }
    

    Usage:

    parse('20120401');
    

    UPDATE:

    As Rocket said, months are 0-based in js...use this if month's aren't 0-based in your string

    function parse(str) {
        if(!/^(\d){8}$/.test(str)) return "invalid date";
        var y = str.substr(0,4),
            m = str.substr(4,2) - 1,
            d = str.substr(6,2);
        return new Date(y,m,d);
    }
    

    UPDATE:

    More rigorous checking for validity of date. Adopted HBP's way to validate date.

    function parse(str) {
        var y = str.substr(0,4),
            m = str.substr(4,2) - 1,
            d = str.substr(6,2);
        var D = new Date(y,m,d);
        return (D.getFullYear() == y && D.getMonth() == m && D.getDate() == d) ? D : 'invalid date';
    }
    
    0 讨论(0)
  • 2020-11-27 22:56

    combining HBP's answer and this answer to get a function that parses YYYYMMDDHHmm and here is a fiddle

    var parseTS=function(str){
                    // validate year as 4 digits, month as 01-12, and day as 01-31
                    if ((str = str.match (/^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])([0-5]\d)$/))) {
                        // make a date
                        str[0] = new Date (+str[1], +str[2] - 1, +str[3], +str[4], +str[5]);
                        // check if month stayed the same (ie that day number is valid)
                        if (str[0].getMonth () === +str[2] - 1) {
                            return str[0];
                        }
                    }
                    return undefined;
                };
    
        console.log(parseTS('201501012645'));
    
    0 讨论(0)
提交回复
热议问题