matt3224
matt3224

Reputation: 103

Convert String To Date Javascript

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

Answers (2)

mplungjan
mplungjan

Reputation: 178393

Off the top of my head

JSFiddle

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

Mitya
Mitya

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

Related Questions