2

I have the following dates as strings..

2016-10-14
2016-10-15
2016-10-16
2016-10-17
2016-10-18
2016-10-19
2016-10-20

How can I dynamically convert them to the following in JavaScript?

14 Oct
15 Oct
16 Oct
...
TheAuzzieJesus
  • 587
  • 9
  • 23
  • 1
    have a look at this thread http://stackoverflow.com/questions/11591854/format-date-to-mm-dd-yyyy-in-javascript, and its a duplicate of http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript – Michael Whinfrey Oct 20 '16 at 01:39
  • use the javascript `Date` type object – Jaromanda X Oct 20 '16 at 01:39
  • @JaromandaX—there is no need to use a Date. – RobG Oct 20 '16 at 03:19
  • True @RobG - but there's no reason not to either (I know, there's a dozen reasons not to, because javascript dates make the baby jesus cry) – Jaromanda X Oct 20 '16 at 03:32
  • @JaromandaX—there are many reasons not to, the best is that it is easier (fewer cross–implementation issues, no Date issues) to create a robust function without it. The first four answers here depend on inconsistent parsing and implementation dependent methods, some produce the wrong results in all hosts, others will produce wrong results in at least some hosts. – RobG Oct 20 '16 at 04:28
  • I did add that there are a dozen reasons not to use Date :p – Jaromanda X Oct 20 '16 at 04:29

3 Answers3

1

results=[]
string=['2016-10-14', '2016-10-15', '2016-10-16', '2016-10-17', '2016-10-18', '2016-10-19', '2016-10-20']
for(var i=0;i<string.length;++i){
  date=new Date(string[i]);
  list=date.toUTCString().split( " ")
results.push(list[1]+" "+list[2])
}


console.log(results)
repzero
  • 8,254
  • 2
  • 18
  • 40
  • The value returned by *toUTCString* is implementation dependent, there is no reason for the above to return the required result in all implementations. – RobG Oct 20 '16 at 03:17
1
var dates = ['2016-10-14', '2016-10-15', '2016-10-16', '2016-10-17', '2016-10-18', '2016-10-19', '2016-10-20'];

function convertDates(str) {
  var d = new Date(str);
  var day = d.getDate();
  var month = d.toString().split(' ')[1];
  return day +' '+ month;
}

var final = [];

dates.map(function(item) {
  final.push(convertDates(item)); 
});

That's it!

Pablo Darde
  • 5,844
  • 10
  • 37
  • 55
  • This will parse the string to a date then create an implementation dependent string that is then reformatted to another string. Why not just reformat the original string? – RobG Oct 20 '16 at 04:29
0

There is no need for a Date object, and certainly you should not parse strings with the Date constructor.

Note that:

new Date('2016-10-14')

will return a date for 13 October for users with a time zone west of Greenwich.

Just reformat the string:

/* Reformat a date like 2016-10-14 as dd MMM
** @param {string} s - date string to reformat
** @returns {string}
*/
function formatDDMMM(s) {
  var months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
  var b = s.split(/\D/);
  return b[2] + ' ' + months[b[1]-1];
}

console.log(formatDDMMM('2016-10-14'));
RobG
  • 142,382
  • 31
  • 172
  • 209