JS——日期字符串yyyymmdd转yyyy-mm-dd的两种方法
时间:2024-04-02 15:25:43 来源:网络cs 作者:淼淼 栏目:电商平台 阅读:
方法一:正则表达式
1、年月日转换
var oldDate = "20111220";
var newDate = oldDate.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3");
console.log(newDate );//2011-12-20
2、年月日时分秒转换
var oldDate = "20111220101010";
var newDate = oldDate.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, "$1-$2-$3 $4:$5:$6");
console.log(newDate );//2011-22-33 10:10:10
方法二:字符串处理
function formatStringyyyyMMddToyyyy_MM_dd (value){
if(value.length == 8){
return value.substring(0, 4) + "-" + value.substring(4, 6) + "-" + value.substring(6, 8);
} else if(value.length == 6){
return value.substring(0, 4) + "-" + value.substring(4, 6);
} else {
return value;
}
}
本文链接:https://www.kjpai.cn/news/2024-04-02/152729.html,文章来源:网络cs,作者:淼淼,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!
下一篇:返回列表