0

I need to subtract 365 days from the current date. I am not sure what is wrong in my date, it prints wierd year "5/17/111" instead of 5/17/2011. Can someone suggest me where I went wrong

currentTime.setDate(currentTime.getDate() - 365);
        var minDay = currentTime.getDate();
        var minMonth = currentTime.getMonth() + 1;
        var minYear = currentTime.getYear();

    minDate = minMonth + '/' + minDay + '/' + minYear;

5 Answers 5

5

You need to use getFullYear() instead of getYear()

var minYear = currentTime.getFullYear();
2

How about:

var minDay = currentTime.getDate();
var minMonth = currentTime.getMonth() + 1;
var minYear = currentTime.getFullYear() - 1;

minDate = minMonth + '/' + minDay + '/' + minYear;
1
  • But what if you calculate this in a leap year on February, 30? The result will be impossible. Edit: Wait - this also applies for the user's original code and December as the month.
    – YMMD
    Commented May 16, 2012 at 21:39
0

you need to be using getFullYear instead of getYear

0

Moment.js would be helpful in this case.

moment().subtract('days', 365).format('MM/DD/YYYY');
0

You might use getFullYear() instead of getYear().

You can read getYear() description here: http://docs.oracle.com/cd/E19957-01/816-6408-10/date.htm#1194138

Not the answer you're looking for? Browse other questions tagged or ask your own question.