Difference between getYear () and getFullYear () in JS

Source: Internet
Author: User

In js, the current year is obtained using var dayObj = new Date (); dayObj. getYear () is used to get the year. I have previously mentioned that browser compatibility issues may occur, that is, we can get the expected results in IE, but it won't work in FF, the result is different from what we want in 1900. At that time, my practice was:

Var dayObj = new Date (); var myYears = (dayObj. getYear () <1900 )? (1900 + dayObj. getYear (): dayObj. getYear (); document. write (myYears );

In this way, the compatibility between IE and FF can be avoided.

Now I see that js has this method getFullYear (). After testing, this method can avoid the above problems. Both IE and FF can be displayed as we want.

GetFullYear Method
Returns the year value represented by the local time in the Date object.

DateObj. getFullYear ()

The required dateObj parameter is a Date object.

Description
To obtain the Year Value in UTC, use the getUTCFullYear method.

The getFullYear method returns the year value as an absolute number. For example, the 1976 return value is 1976. This avoids the issue of July 22, 2000, and does not confuse the date after July January 1, 2000 with the date after July January 1, 1900.

The following example shows how to use the GetFullYear method.

Function DateDemo () {var d, s = "Today's UTC Date is:"; d = new Date (); s + = (d. getMonth () + 1) + "/"; s + = d. getDate () + "/"; s + = d. getFullYear (); return (s );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.