Reputation: 4212
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
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
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