-1

I have a date in the format yyyymmdd. Ex: 20141004

What I need to do is to convert it like dd-mmm-yyyy. Ex: 4 Oct 2014

There is a similar question here: JavaScript convert string into Date with format (dd mmm yyyy) i.e. 01 Jun 2012

The difference is that I have no "/" character to split by.

Another limitation is that I cannot use any external library, as this code is used inside another tool, so I need native JS.

Community
  • 1
  • 1
Sorin
  • 31
  • 6

1 Answers1

0

I did it like this:

var date = "20141004";
var y = date.substring(0, 4);
var m = parseInt(date.substring(4, 6), 10);
var d = date.substring(6, 8);

var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

mm = months[m - 1];

document.write(d + " " + mm + " " + y);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Sorin
  • 31
  • 6