0

I have the following javascript code:

<script type="text/javascript">
    $(function () {
        var currentDateTime = new Date();
        var oneYear = new Date();
        oneYear.setYear(oneYear.getYear() + 1);
        alert(currentDateTime + "_" + oneYear);
    });
</script>

i would expect the alert to output the current datetime and the datetime of one year from now. However I get this in the alert: "Fri Oct 22 2010 14:17:31 GMT-0400 (Eastern Daylight Time)_Thu Oct 22 0111 14:17:31 GMT-0400 (Eastern Daylight Time)"

Clearly it's not adding "1" to the Year correctly!

Whats going on? How did it become the year 0111???

1

3 Answers 3

14

It is correct. .getYear() returns "actual year − 1900". 2010 − 1900 = 110.

Use .getFullYear() instead. .getYear() has been deprecated for a long time.

Sign up to request clarification or add additional context in comments.

Comments

3

Y2K was 10 years ago, but you're still using getYear instead of getFullYear? tsk tsk...

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getFullYear

Comments

1

Instead of .getYear() try .getFullYear()

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.