Home >Web Front-end >JS Tutorial >Solve the browser compatibility issue of js Date() date function
This article mainly introduces the solution to the browser compatibility problem of the js Date() date function. Friends who need it can refer to it
Generally, direct new Date() will not cause compatibility problems, but new Date(datetimeformatstring) browser compatibility issues often arise. Why, some formats in datetimeformatstring are not compatible with browsers.
1. No parameters
//无参 所有浏览器都兼容 var dateTime = new Date();
2.Date parameter
//日期参数 格式1 var dateTime = new Date("2017-09-12");
a. IE
> IE9-(not compatible)
> IE9+(compatible, including IE9)
b. Compatible)
c. Google (Compatible)
//日期参数 格式2 所有浏览器都兼容 var dateTime = new Date("2017/09/12");
3. Date and time parameters
//日期时间参数 格式1 var dateTime = new Date("2017-09-12 13:41:00");
a. IE (not compatible, no matter which version)
b. Firefox (not compatible)
c. Google (compatible)
//日期时间参数 格式2 所有浏览器都兼容 var dateTime = new Date("2017/09/12 13:42:00");
//日期时间参数 格式3 var dateTime = new Date("2017-09-12T13:42:00");
a. IE
> IE9- (not compatible)
> IE9+ (compatible)
> IE9 (semi-compatible with 8 hours time difference)
b. Firefox (compatible)
c. Google (semi-compatible with 8 hours time difference)
//日期时间参数 格式4 var dateTime = new Date("2017/09/12T13:42:00");
a. IE (semi-compatible with 1 hour time difference)
b. Firefox (not compatible)
c. Google (not compatible)
To sum up, the format supported by all major browsers is: var dateTime = new Date("2017/09/12 13:42:00");
Then the solution is to convert datetimeformatstring into yyyy/MM/dd hh:mm:ss format string
The above is the detailed content of Solve the browser compatibility issue of js Date() date function. For more information, please follow other related articles on the PHP Chinese website!