js把字符串(yyyymmddhhmmss)转换成日期格式(yyyy-mm-dd hh:mm:ss)

方法一:

var r="20190107142208".replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, "$1-$2-$3 $4:$5:$6")
 console.log(r)//yyyy-MM-dd hh:mm:ss

方法二:

var time='20190107142208';
var dtime=Date(time.replace(/^(\d{4})(\d{2})(\d{2})$/,"$1/$2/$3"));//JS格式化为Thu May 12 2017 08:00:00 GMT+0800 (中国标准时间)
var date = new Date(dtime);
date_value=date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
console.log(date_value)//yyyy-MM-dd hh:mm:ss

版权声明:除特别注明外,本站所有文章均为田珊珊个人博客原创

转载请注明:出处来自田珊珊个人博客 » js把字符串(yyyymmddhhmmss)转换成日期格式(yyyy-mm-dd hh:mm:ss)

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注