3

有没有一种简单的方法可以将字符串 "YYYYMMDDHHMMSS" 转换为 "YYYY/MM/DD HH:MM:SS" ?

例如我有

var date_string = "20121231023350";

我希望输出为

new_date_string = "2012/12/31 02:33:50";
4

3 回答 3

6
"20121231023350".replace(
    /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/,
    "$1/$2/$3 $4:$5:$6");  // "2012/12/31 02:33:50"
于 2013-01-09T13:35:23.790 回答
3
function ConvertTime(OldTime)
{
    if(OldTime.length != 14)
        return "Error";

    return OldTime.substring(0,4) + "/" + OldTime.substring(4,6) + "/" + OldTime.substring(6,8) + " " + OldTime.substring(8,10) + ":" + OldTime.substring(10,12) + ":" + OldTime.substring(12,14);
}

ConvertTime("20121231023350");
于 2013-01-09T13:40:40.777 回答
0

使用诸如date.jsMoment.js 之类的库。

这些中的任何一个都应该使日期 Javascript 处理/解析/格式化变得轻而易举。

于 2013-01-09T13:34:42.877 回答