SoFunction
Updated on 2025-03-03

Javascript's similarities and differences sharing

Among them, the getYear() method was born early and used OK in the early days, but after 2000, there were many problems with this method, because on browsers such as Firefox and Safari, getYear always returns the difference between the year and 1900, such as 98 in 1998, and 109 in 2009. If everyone handled this way, it would be better to add it together. Microsoft itself corrected getYear in IE browser, but Firefox (the latest version did not correct this problem) was still kept in the dark and still honestly analyzed getYear according to the original rules. It could have been expected that users would correct it themselves, so it was useless. So getFullYear and getUTCFullYear were born.

1. getYear() function

Use the getYear() method to return two or four digits of years, and the number returned with getYear() is not necessarily 4 digits! The getYear() method between 1900 and 1999 returns only double digits. The returned four-digit numbers before or after this year. For example, in 2009, the Javascript parser should return 2009, while the browser calculates the return 109. This should be an early agreement, and IE has been revising it. This function has been gradually discarded and is not recommended.
Copy the codeThe code is as follows:

<script type="text/javascript">
var d = new Date();
(());//IE output 2009, FIREFOX output 109
</script>


2. getFullYea() function

This problem does not exist when the getFullYear function is used. The getFullYear() method can return a four-digit year, so that everyone (IE and FIREFOX, etc.) do not need to operate, just output the parsed value.
Copy the codeThe code is as follows:

<script type="text/javascript">
var d = new Date();
(());//IE output 2009, FIREFOX output 2009
</script>


3. getUTCFullYear() function

The getUTCFullYear() function returns four digits based on UTC time to represent the year. The theoretical angle is completely different from the getFullYear() method. Although the output is the same for most of the time, if the date of the day is December 31 or January 1, the return value of getUTCFullYear() and getFullYear() may be different, depending on the relationship between the local time zone and UTC general time, that is, the difference.
Copy the codeThe code is as follows:

<script type="text/javascript">
var d = new Date();
(());//IE output 2009, FIREFOX output 2009
</script>

For example, the local time in mainland China, *, Macao, *, *, Singapore, Malaysia, the Philippines and other regions is 8 hours faster than UTC, which is called UTC+8, which means 8 hours faster than UTC time. Similar understanding of subtraction, such as UTC-10, etc.