简体   繁体   中英

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

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 -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM