Home  >  Article  >  php教程  >  How to solve the problem that the Safari browser of JS IOS/iPhone is not compatible with Date() in Javascript

How to solve the problem that the Safari browser of JS IOS/iPhone is not compatible with Date() in Javascript

高洛峰
高洛峰Original
2016-12-07 10:26:431650browse

var date = new Date('2016-12-6 11:11:11');
document.write(date);

Recently I am writing a time judgment script, which needs to convert the fixed string time into a timestamp for comparison. When doing this, I am used to using chrome as a debugging tool. After the code is basically completed, everything is normal;

Use other browsers Well, IE and Safari are not compatible and return the error "Invalid Date".

I thought it might be a problem with the string format, so I changed it to '2016/11/11 11:11:11' and tested again. The result was normal. I thought this should be no problem. Then I used the mobile browser to continue accessing. Android is normal. The iPhone continued to report an error.

Then I changed "Nov 11 2016 11:11:11" and it still reported an error. No matter how I changed it, it didn't work. After racking my brains, I finally saw such a solution on the forum:

var arr = "2016/11/11 11:11:11".split(/[- : \/]/),
date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
document.write(date);

Finally compatible with all browsers, conclusion:

Safari in iPhone cannot interpret time formats such as YYYY-MM-DD HH:mm:ss or YYYY/MM/DD HH:mm:ss, while Google Firefox, etc. The browser has extended such a format. The formats supported by

Safari on the iPhone are YYYY, MM, DD, HH, mm, ss. This problem has bothered me for most of the day. I really want to attract Apple programmers. Going out and shooting people for 10 minutes is so fucking unique. Annoying

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