最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatting YYYYMMDDHHMMSS to YYYYMMDD HH:MM:SS - Stack Overflow

programmeradmin0浏览0评论

Is there a simple way to convert a string "YYYYMMDDHHMMSS" to "YYYY/MM/DD HH:MM:SS" ?

For example I have

var date_string = "20121231023350";

And I would like the output as

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

Is there a simple way to convert a string "YYYYMMDDHHMMSS" to "YYYY/MM/DD HH:MM:SS" ?

For example I have

var date_string = "20121231023350";

And I would like the output as

new_date_string = "2012/12/31 02:33:50";
Share Improve this question asked Jan 9, 2013 at 13:32 skosskos 4,2428 gold badges38 silver badges59 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7
"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"
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");

Use a library such as date.js or Moment.js.

Either of these should make date Javascript handling/parsing/formatting a doddle.

发布评论

评论列表(0)

  1. 暂无评论