Reputation: 103
how can i convert the following string to the required format in javascript:
2013-12-01 03:22:03 = 01 Dec '13
Thanks
Upvotes: 0
Views: 119
Reputation: 178393
Off the top of my head
function conv(str) {
var d=new Date(str.replace(/-/g,"/"));
var day = d.getDate();
if (day<10) day ="0"+day;
var yyyy=""+d.getFullYear();
return ""+day +" "+["Jan","Feb",..."Dec"][d.getMonth()]+" '"+yyyy.substring(2);
}
var dStr = conv("2013-12-01 03:22:03");
The reason to convert from - to / is that not all browsers accept the format with -
Upvotes: 1
Reputation: 34598
new Date('2013-12-01 03:22:03');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Upvotes: 1