JS getyear () and getFullYear () difference Analysis _ Time and date

Source: Internet
Author: User

JS to get the current year practice is Var dayobj=new Date (); Dayobj.getyear () to get the year, I have written earlier this will appear browser compatibility problem, is in IE can get the results we want but not in FF, and we want the results of 1900 years difference. At the time, my approach was:

var dayobj=new Date ();
var myyears = (Dayobj.getyear () < 1900)? (1900 + dayobj.getyear ()): Dayobj.getyear ();
document.write (Myyears);

This avoids the compatibility problems of IE and ff.

Now I see in JS there is such a method getFullYear (). To test, the original method can avoid the above problems, IE and FF can be as we want to show.

getFullYear method
returns the year value in a Date object that is expressed in local time.

Dateobj.getfullyear ()

The required option Dateobj parameter is a Date object.

Description
To obtain a year value expressed in global standard Time (UTC), use the getUTCFullYear method.

The getFullYear method returns the year value as an absolute number. For example, the return value for 1976 is 1976. This would avoid a 2000 problem and would not confuse the date after January 1, 2000 with the date after January 1, 1900.

The following example illustrates the use of the getFullYear method.

function Datedemo () {  
var d, s = "Today 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.