Why does this javascript return 108 instead of 2008? it gets the day and month correct but not the year?
myDate = new Date();
year = myDate.getYear();
year = 108?
Why does this javascript return 108 instead of 2008? it gets the day and month correct but not the year?
myDate = new Date();
year = myDate.getYear();
year = 108?
You should, as pointed out, never use
getYear()
, but instead usegetFullYear()
.The story is however not as simple as "IE implements
GetYear()
asgetFullYear()
. Opera and IE these days treatgetYear()
asgetYear()
was originally specified for dates before 2000, but will treat it asgetFullYear()
for dates after 2000, while webkit and Firefox stick with the old behaviorThis outputs 99 in all browsers:
This outputs 108 in FF/WebKit, and 2008 in Opera/IE:
I am using
date.getUTCFullYear()
; working without problems.It's a Y2K thing, only the years since 1900 are counted.
There are potential compatibility issues now that
getYear()
has been deprecated in favour ofgetFullYear()
- from quirksmode:There are also implementation differences between Internet Explorer and Firefox, as IE's implementation of
getYear()
was changed to behave likegetFullYear()
- from IBM:Check the docs. It's not a Y2K issue -- it's a lack of a Y2K issue! This decision was made originally in C and was copied into Perl, apparently JavaScript, and probably several other languages. That long ago it was apparently still felt desirable to use two-digit years, but remarkably whoever designed that interface had enough forethought to realize they needed to think about what would happen in the year 2000 and beyond, so instead of just providing the last two digits, they provided the number of years since 1900. You could use the two digits, if you were in a hurry or wanted to be risky. Or if you wanted your program to continue to work, you could add 100 to the result and use full-fledged four-digit years.
I remember the first time I did date manipulation in Perl. Strangely enough I read the docs. Apparently this is not a common thing. A year or two later I got called into the office on December 31, 1999 to fix a bug that had been discovered at the last possible minute in some contract Perl code, stuff I'd never had anything to do with. It was this exact issue: the standard date call returned years since 1900, and the programmers treated it as a two-digit year. (They assumed they'd get "00" in 2000.) As a young inexperienced programmer, it blew my mind that we'd paid so much extra for a "professional" job, and those people hadn't even bothered to read the documentation. It was the beginning of many years of disillusionment; now I'm old and cynical. :)
In the year 2000, the annual YAPC Perl conference was referred to as "YAPC 19100" in honor of this oft-reported non-bug.
Nowadays, in the Perl world at least, it makes more sense to use a standard module for date-handling, one which uses real four-digit years. Not sure what might be available for JavaScript.
This question is so old that it makes me weep with nostalgia for the dotcom days!
That's right, Date.getYear() returns the number of years since 1900, just like Perl's localtime(). One wonders why a language designed in the 1990s wouldn't account for the century turnover, but what can I say? You had to be there. It sort of made a kind of sense at the time (like pets.com did).
Before 2000, one might have been tempted to fix this bug by appending "19" to the result of getYear() resulting in the "year 19100 bug". Others have already answered this question sufficiently (add 1900 to the result of getDate()).
Maybe the book you're reading about JavaScript is a little old?
Thanks for the blast from the past!
The number you get is the number of years since 1900. Don't ask me why..