0

how can i convert the following string to the required format in javascript:

2013-12-01 03:22:03 = 01 Dec '13

Thanks

4
  • Did you try just passing that string to new Date() Commented Mar 14, 2014 at 22:33
  • 1
    This question appears to be off-topic because it can be solved by simply passing the string to new Date() Commented Mar 14, 2014 at 22:34
  • Are you asking how to get the Date(2013-12-01 03:22:03) to print as "01 Dec '13"? Commented Mar 14, 2014 at 22:34
  • Certainly looks like it. Not all browser will accept the dashes in the date by the way. Commented Mar 14, 2014 at 22:50

2 Answers 2

1

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 -

Sign up to request clarification or add additional context in comments.

Comments

1
new Date('2013-12-01 03:22:03');

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.