js把字符串(yyyymmdd)转换成日期格式(yyyy-mm-dd)

js把字符串(yyyymmdd)转换成日期格式(yyyy-mm-dd),
例如把19930701转换成1993-07-01

这个字符串不是标准的日期类型,估计只能字符串分割再填充了。

var dateString = '19930701';
var pattern = /(\d{4})(\d{2})(\d{2})/;
var formatedDate = dateString.replace(pattern, '$1-$2-$3');
console.log(formatedDate);

或者直接:

var r="20112233".replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3")
console.log(r)

你可能感兴趣的:(JS)