getYear Date function of javascript gives alert as 111


Monday, February 7, 2011

Hello friends,

You might have encountered this issue many times while using datetimepicker on your modules.

For example, Datetimepicker is provideing you some other format of date and you want to display in some other format

or you want to add date, month or year to original date.

For example, if you are making alert of current date like below code then for date and month, you will get correct data
but for year you will wrong data.

so this you will be doing like this

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

For today alert you will get
Mon Feb 07 2011 11:52:21 GMT+0530 (India Standard Time)

For today.getMonth() alert, you will get,
02

For today.getYear() alert you will get,
111

which is wrong, that should alert 2011,

This is because getYear method returns the year minus 1900,


Solution to this,

Instead of getYear, Please use getFullYear javascript function,
this is give you current year as 2011

This issue mostly has been seen in Mozila browser.


Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Senior Asp.Net Developer

Email: raj143svmit@gmail.com
e-Procurement Technologies Ltd (India)
www.abcprocure.com


2 comments :

Anonymous said...

Works great, thanks. BTW, it also happens in IE9.

Anonymous said...

thanks very much!!