Home >Web Front-end >JS Tutorial >Solution to the problem that the year cannot be displayed normally in Firefox_javascript skills

Solution to the problem that the year cannot be displayed normally in Firefox_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:37:171322browse

We will see this display on some websites when using Firefox:
The current time is: January 26, 108

The IE browser displays normally:
The current time is: January 26, 2008

The reason is the compatibility issue of javascript

var today = new date();var year = today.getYear();

In Firefox, getYear returns the value of "current year-1900", but Microsoft has made a change:
When the year of today is greater than or equal to 2000, 1900 is added directly and the returned 200X (instead of 10X)
For example: today’s year is 1999, return 99
Today’s year is 2000. Return 2000

A simple solution is to add a judgment:

year = (year<1900&#63;(1900+year):year);

There is also another method:
Call
through getFullYear getUTCFullYear

var year = today.getFullYear();
Statement:
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