0

I have a date defined as "Jul 2015" or "Aug 2016" and I need to get the last year version of them.

For example:

getLastYear("Jul 2015")
return: Jul 2014

How do I do it in JavaScript? My attempt so far is not working:

var date = new Date('Jul 2015');
var new_date = new Date().setYear(date .getFullYear()-1);
alert(new_date );

// return a timestamp value e.g. 1404505003030
levi
  • 19,165
  • 7
  • 56
  • 68
nogias
  • 543
  • 1
  • 4
  • 18

5 Answers5

2

You almost got it, you missed convert timestamp to date

var date = new Date('Jul 2015');
var new_date = new Date().setYear(date .getFullYear()-1);
    new_date = new Date(new_date)

To format your date, use this:

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


var monthIndex = new_date.getMonth();
var month = months[monthIndex]
var year = new_date.getYear();
var final_date = year + month
console.log(final_date) // you will get 'Jul 2014'
levi
  • 19,165
  • 7
  • 56
  • 68
0
var date = new Date('Jul 2015');
date.setYear(date .getFullYear()-1);
alert(date);

Should do it for you. setYear returns a value, and sets the year on the date, so you need to use it accordingly.

Saransh Kataria
  • 1,329
  • 9
  • 16
  • That works, but is there anyway I could convert that date format to the same format as the input i.e. Jul 2014? – nogias Nov 02 '15 at 06:51
  • Have a look at this detailed explanation http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – J-D Nov 02 '15 at 06:54
  • There are a couple of ways, you can do a date.toDateString() and then substring it to what you want to get, or you can use date.getMonth() and date.getYear() and append them to a string – Saransh Kataria Nov 02 '15 at 06:55
0

you need to do something like this, you can edit month array according to your requirements,

  function getLastYear(date){
  var month = new Array();
  month[0] = "January";
  month[1] = "February";
  month[2] = "March";
  month[3] = "April";
  month[4] = "May";
  month[5] = "June";
  month[6] = "July";
  month[7] = "August";
  month[8] = "September";
  month[9] = "October";
  month[10] = "November";
  month[11] = "December";
  var d = (new Date(new Date('1 '+date).setYear(new Date('1 '+date).getFullYear() -1)))
  console.log(d);
  return month[d.getMonth()]+' '+d.getFullYear()
}

getLastYear("July 2016") //"July 2015"
Shailendra Sharma
  • 6,835
  • 2
  • 24
  • 42
0

You actually dont need a date Object for that. You also could use string functions for that:

var dateString = "Jul 2015";
var newString = dateString.substr(0,dateString.length - 4)+parseInt(dateString.substr(dateString.length - 4)-1);
alert(newString);
Niczem Olaske
  • 241
  • 4
  • 13
0
var date = new Date('Jul 2015');
var new_date = new Date(date .getFullYear()-1,date.getMonth());
var year = new_date.getFullYear();
Date.prototype.getMonthName = function() {
          var monthNames = [ "Jan", "Feb", "Mar", "April", "May", "Jun", 
                        "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ];
          return monthNames[this.getMonth()];
     }
var month  = new_date.getMonthName(new_date.getMonth());
alert(month+" "+year );

also you can use moment library of javascript to convert to and from diffrent format http://momentjs.com/ reference:

Community
  • 1
  • 1