js中字符串如何变成时间格式样子,谢谢!

兄弟姐妹帮我讲解下,js中字符串如何变成时间格式样子,谢谢!?

var occurdate="20191231";
var occurtime="100850";

请问有什么方便的写法,可以变成时间样式
occurdate="2019-12-31";
occurtime="10:08:50";
最新回答
轻飞曼舞

2024-12-01 23:23:07

var reg1 = /(\d{4})(\d{2})(\d{2})/;
var reg2 = /(\d{2})(\d{2})(\d{2})/;
var occurdate="20191231";
var occurtime="100850";
occurdate = occurdate.replace(reg1, '$1-$2-$3');
occurtime = occurtime.replace(reg2, '$1:$2:$3');
console.log(occurdate);
console.log(occurtime);
虐爆托儿所

2024-12-01 20:27:06

字符串截取加拼接
occurdate.substr(0,4)+'-'+occurdate.substr(4,2)+'-'+occurdate.substr(6,2)//日期
occurtime.substr(0,2)+':'+occurtime.substr(2,2)+':'+occurtime.substr(4,2)//时间