兄弟姐妹帮我讲解下,js中字符串如何变成时间格式样子,谢谢!? var occurdate="20191231";var occurtime="100850";请问有什么方便的写法,可以变成时间样式occurdate="2019-12-31";occurtime="10:08:50";
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);
字符串截取加拼接occurdate.substr(0,4)+'-'+occurdate.substr(4,2)+'-'+occurdate.substr(6,2)//日期occurtime.substr(0,2)+':'+occurtime.substr(2,2)+':'+occurtime.substr(4,2)//时间