字符串转时间

//匹配时间格式:20190322T142523910(严格匹配)
var DATE_REGEXP = new RegExp("^(\\d{4})(\\d{2})(\\d{2})([T\\s](\\d{2})(\\d{2})(\\d{2})(\\d{3}))$");   
 
function toDate(dateString) {       
    if (DATE_REGEXP.test(dateString)) {           
        var timestamp = dateString.replace(DATE_REGEXP, function($all, $year, $month, $day, $part1, $hour, $minute, $second, $part2, $milliscond) {               
            var date = new Date($year, $month - 1, $day, $hour || "00", $minute || "00", $second || "00", $milliscond || "00");               
            return date.getTime();           
        });           
        var date = new Date();           
        date.setTime(timestamp);    
     var year = date.getFullYear();
     var month = date.getMonth() + 1;
     var day = date.getDate();
     if (month < 10) {
    month = "0" + month;
     }
     if (day < 10) {
    day = "0" + day;
     }
      return year + "-" + month + "-" + day;

  }
  return null;
}

  

posted @ 2021-02-20 13:41  suomiao  阅读(232)  评论(0编辑  收藏  举报