Home > Web Front-end > JS Tutorial > Sharing the similarities and differences of Javascript's getYear, getFullYear, and getUTCFullYear_javascript skills

Sharing the similarities and differences of Javascript's getYear, getFullYear, and getUTCFullYear_javascript skills

WBOY
Release: 2016-05-16 17:58:56
Original
1323 people have browsed it

Among them, the getYear() method was born earlier, and OK has been used in the early days. However, this method has many problems after 2000, because on browsers such as Firefox and Safari, getYear always returns the difference between the year and 1900, such as 1998 The year returns 98, while 2009 will display 109. If everyone does it this way, they have to add it all up. Microsoft itself has corrected getYear in IE, but Firefox (the latest version has not corrected this problem) still has They were kept in the dark and still parsed getYear according to the original rules. They could have expected users to correct it themselves, but it was useless, so getFullYear and getUTCFullYear were born.

1. getYear() function

Use the getYear() method to return a two-digit or four-digit year. The number returned by getYear() is not necessarily It’s 4 digits! The getYear() method between 1900 and 1999 returns only two digits. Years before or after are returned as four-digit numbers. For example, in 2009, the Javascript parser should return 2009, while the browser calculates and returns 109. This should be an early agreement, but IE changed it. This function has been gradually deprecated and is not recommended.

Copy code The code is as follows:




2. getFullYea() function

getFullYear function does not have this problem. The getFullYear() method can return a four-digit year, so that everyone (IE, FIREFOX, etc.) does not need to perform calculations and can directly output the parsed value.
Copy code The code is as follows:




3. getUTCFullYear() function

getUTCFullYear() function returns four digits to represent the year based on UTC time. The theoretical perspective of the getFullYear() method is completely different. Although the output is the same most of the time, if the current date is December 31st or January 1st, the return value of getUTCFulYear() is the same as the return value of getFullYear() It may be different, depending on the relationship between the local time zone and UTC universal time, which is the difference.
Copy code The code is as follows:



For example, the local time in mainland China, Hong Kong, Macau, Mongolia, Taiwan, Singapore, Malaysia, Philippines and other regions is 8 hours ahead of UTC, and is recorded as UTC 8, which means 8 hours ahead of UTC time. Subtraction is similar to understanding, such as UTC-10, etc.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template