Search the knowledgebase


JavaScript getYear() in Opera not returning the full year

When working with JavaScript date objects programmers need to be aware of the differences between the various browsers in handling the getYear() function.

The specifications for JavaScript indicate that this function should return years since 1900 and that the function getFullYear() should be used to retrieve a full, 4-digit year. Internet Explorer sometimes returns all four digits when getYear() is used. Thus the following code

var today = new Date();
alert(today.getYear())

run in 2001 will return "2001" in IE and "101" in Opera. This behaviour is by design for backwards compatibility with standards-compatible scripts. If you need a 4-digit year in Opera, use getFullYear().


Browse through articles in the same categories: unsorted