js yyyyMMdd转日期date时间戳
原创    running    发表于:2019-01-21 10:43:58
  阅读 :50   收藏   编辑
/**
 * dateStr : yyyyMMdd or yyyMMddhhmmss
 * @param dateStr
 * @returns {Date}
 */
function formatDateStr(dateStr){
   if(dateStr == null ) return;
   var dateStrTmp = "";
   if(dateStr.length == 8){
      dateStrTmp += dateStr + "000000";
   }

   if(dateStrTmp.length == 14){
      var pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
      return new Date(dateStrTmp.replace(pattern, '$1-$2-$3 $4:$5:$6'));
   }
}
console.log('dateStr:20190121:' + formatDateStr("20190121"));
//dateStr:20190121:Mon Jan 21 2019 00:00:00 GMT+0800 (中国标准时间)


评论
条评论