将YYYYMMDDHHMMSS格式化为YYYY / MM / DD HH:MM:SS

时间:2013-01-09 13:32:01

标签: javascript

有没有简单的方法将字符串“YYYYMMDDHHMMSS”转换为“YYYY / MM / DD HH:MM:SS”?

例如我有

var date_string = "20121231023350";

我希望输出为

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

3 个答案:

答案 0 :(得分: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"

答案 1 :(得分: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");

答案 2 :(得分:1)

使用date.jsMoment.js等库。

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