skos
skos

Reputation: 4212

Formatting YYYYMMDDHHMMSS to YYYY/MM/DD HH:MM:SS

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";

Upvotes: 3

Views: 3999

Answers (3)

ZombieSpy
ZombieSpy

Reputation: 1376

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");

Upvotes: 3

VisioN
VisioN

Reputation: 145368

"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"

Upvotes: 7

SDC
SDC

Reputation: 14222

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

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

Upvotes: 0

Related Questions