-1

I have a date format that is like this

"5-2015"

How can I convert it so that it appears as "May 2015" on screen?

dave
  • 355
  • 5
  • 11
  • Find a solution that requires neither a library, nor that you define month names on your own, below in my answer. It is ready to be copied & pasted into your project's code. – connexo Aug 05 '15 at 16:21

3 Answers3

1

You could try MomentJS http://momentjs.com/ which is a Date/Time library for Javascript. I believe the syntax would be moment(yourDate, 'M-YYYY').format('MMM YYYY');

If you want to roll your own:

function format(date) {
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

    var month = date.substring(0, date.indexOf('-'));
    var year = date.substring(date.indexOf('-') + 1);

    return months[parseInt(month) - 1] + ' ' + year;
}

var formatted = format('5-2015');
Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68
  • Useful if you have more dates inside your app, but over kill, in my opinion, if all you have to handle is the case stated in the question. – GillesC Aug 05 '15 at 15:56
  • Granted, but it's not stated in his question. – Patrick Grimard Aug 05 '15 at 15:57
  • this is pretty much what you want, momentjs is almost always recommend for questions like this. – Huang Chen Aug 05 '15 at 15:58
  • @gillesc is right, this is the only date I have to handle - is moment.js too heavy in this case? – dave Aug 05 '15 at 16:00
  • If that's the case, you could just write your own function which parses the string and returns the format you want. – Patrick Grimard Aug 05 '15 at 16:01
  • Just think in general when a question doesn't ask for a library recommendation, which would be off topic, answers shouldn't be just use this library. Learning how to use library is great, understanding what they do in the back ground is even more useful. – GillesC Aug 05 '15 at 16:01
  • I've updated my response with an alternative with no library. – Patrick Grimard Aug 05 '15 at 16:04
0

Assuming all of your dates are in the format of "5-2015" (e.g. 6-2015 or 12-2015), what you can do is use the split function on javascript split the string value into months and years.

For example:

var date = "5-2015";
date.split("-"); //splits the date whenever it sees the dash
var month;
if(date[0] == "5"){ //accesses the first split value (the month value) and check if it's a month.
    month = "May";
} //do this with the rest of the months.
var finalString = month + " " + date[1]; //constructs the final string, i.e. May 2015
alert(finalString);
David Ma
  • 59
  • 11
0

You can go like this:

function parseDate(dateString) {
  var d = dateString.split("-"); // ["5", "2015"]
  // Month is 0-based, so subtract 1
  var D = new Date(d[1], d[0]-1).toString().split(" "); // ["Fri", "May", "01", "2015", "00:00:00", "GMT+0200", "(Central", "Europe", "Daylight", "Time)"]
  return D[1] + " " + D[3];
}

(function() {
  alert(parseDate("5-2015"));
}());
connexo
  • 53,704
  • 14
  • 91
  • 128