Home > Web Front-end > JS Tutorial > body text

Pitfalls encountered by JavaScript's new date and other date functions in Safari

高洛峰
Release: 2016-12-09 10:56:41
Original
1162 people have browsed it

When I was working on mobile Web recently, I successfully debugged it with Chrome on the PC, but strange problems appeared when I tested it on the iPhone. After a series of debugging, it was found that there was a problem related to the date. At first I suspected it was a problem in the production environment, but the same problem also occurred when debugging locally with the Mac version of Safari. After consulting some information, I found that there is a strange problem in Safari's support for JavaScript's new Date function.

Usually, due to being accustomed to the datetime format in SQL, dates are formatted in yyyy-mm-dd. However, Safari does not support such a format, so when you enter the following statement, an empty time will be returned.

new Date('2016-04-17');
Copy after login

The format supported by Safari is yyyy/mm/dd. The main difference lies in the dash - and the slash /, and the slash format can also run normally in other common browsers. , so you just need to replace them with slashes. The conversion function is as follows:

new Date('2016-04-17'.replace(/-/g, "/"));
Copy after login


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!