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?
var date_object=new Date(); var year = date_object.getYear(); if(year < 2000) { year = year + 1900; } //u will get the full year ....
BTW, different browsers might return different results, so it's better to skip this function altogether and and use getFullYear() always.
use
date.getFullYear()
.This is (as correctly pointed out elsewhere) is a Y2K thing. Netscape (written before 2000) originally returned, for example
98
fromgetYear()
. Rather than return to00
, it instead returned100
for the year 2000. Then other browsers came along and did it differently, and everyone was unhappy as incompatibility reigned.Later browsers supported
getFullYear
as a standard method to return the complete year.It must return the number of years since the year 1900.
It's dumb. It dates to pre-Y2K days, and now just returns the number of years since 1900 for legacy reasons. Use getFullYear() to get the actual year.
Since getFullYear doesn't work in older browsers, you can use something like this:
Javascript prototype can be used to extend existing objects, much like C# extension methods. Now, we can just do this;