About support for the JS Date object in the Safari browser

Source: Internet
Author: User

Application blog for several months, has been trying to write something but always feel that their writing is not good, has been stranded. Don't say much nonsense, get to the point!

Support for JS Date object in iOS and Safari browser

1. The problem scenario is that you need to display different content at a specific time.

There are two ways to solve this problem:

1. Time to get the server through the background.

In the return to the front end of the logo, the front end through the template, or other way to judge or display different content. We don't care about this.

2. Show different content by local acquisition system time.

That's the way I do it, but there's a problem here. Attached code

function Setcontentwithtime (time) {      varnew  date (),          new  Date (time) . GetTime ();       var New Date (current.getfullyear () + '-' + (Current.getmonth () +1) + '-' +current.getdate ()). GetTime ();       if (Curtime = = setDate) {      ...      } Else {      ...      }        
Setcontentwithtime ("2015-8-13");

The above code runs in Chrome and FF without problems. But there was a problem running in Safari.

The problem is as follows:

OK, the problem arises, in safari it is impossible to change the form of a date like "Xxxx-xx-xx" in Safari

new Date (' 2015-8-13 ');Invalid Datelater found, Safari can be recognized in the date can only be "xxxx/xx/xx" this form of date, in Safarinew Date (' 2015/8/13 ');Thu 00:00:00 gmt+08003. Similarly, is it possible to imagine that there are problems with iOS systems as well. tests have found that this code also has a problem.

4. The date () conversion of JS is not supported in those iOS systems. Please test, I know that there is still this problem in ios8.4.

5. Mentioned here we can also look at the JS Date object inside the construction method will be found. In fact, we can take another approach to new Date;

查资料发现Date的构造函数:new Date()new Date(milliseconds)new Date(datestring)new Date(yearmonth)new Date(yearmonth,day)new Date(yearmonthdayhours)new Date(yearmonthdayhoursminutes)new Date(year,monthdayhoursminutesseconds)new Date(yearmonthdayhoursminutesseconds,microseconds)
这里需要注意的是:
    • If there are no arguments, the current date is returned
    • If the argument is a number, the number is treated as a millisecond value and converted to a date
    • If the argument is a string, the string is treated as a string representation of the date, converted to a date
    • You can also use six constructors to precisely define and return a time

Conclusion: actually is a small problem, but in the process of doing the project, always make people headache, please do not spray!!! Thank you!

Reference documents:

Http://www.w3school.com.cn/jsref/jsref_obj_date.asp

Http://blog.sina.com.cn/s/blog_6a0cd5e501011so7.html

Http://www.cnblogs.com/keke/p/3372558.html

http://blog.csdn.net/liu510817387/article/details/8688216

About support for the JS Date object in the Safari browser

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.