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
3 Answers
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.