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();
Sign up to request clarification or add additional context in comments.

Comments

2

How about:

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

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

1 Comment

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.
0

you need to be using getFullYear instead of getYear

Comments

0

Moment.js would be helpful in this case.

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.