On the new Date () format pit in Safari

Source: Internet
Author: User
Tags expression iso 8601

Today, in the test, I found the following code in Chrome:

New Date ("2014-03-09");

Error invalid date in Safari. Look up the information to find a similar question and answer:

StackOverflow Address: Http://stackoverflow.com/questions/4310953/invalid-date-in-safari

Interpretation and translation are as follows:

The standard formats that safari can support today are as follows:

Mm-dd-yyyy
Yyyy/mm/dd
Mm/dd/yyyy
MMMM DD, yyyy
MMM DD, yyyy

Datejs is a good repository for formatting non-standard dates.

As to the date date standard, the original interception ECMA-262 standard content is explained, the citation is as follows:

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended format. The format is as Follows:yyyy-mm-ddthh:mm:ss.sssz Where The fields are as follows:

ECMAScript defines a string interchange format for the date time based on the ISO 8601 extended format. The format is YYYY-MM-DDTHH:MM:SS.SSSZ, and each domain is described as follows:

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

YYYY is the decimal digits of the Gregorian calendar. YYYY is the decimal expression of the GMT year
":" (Hyphon) appears literally twice in the string. ":" literally appears between two strings
MM is the month of the "Year" (January) to (December). MM indicates month from 01 (January) to 12 (December)
DD is the "month from" to 31. DD represents the number of days in the month from 01 to 31.
T "T" appears literally in the string and to indicate the beginning of the time element. T literally appears in the string, indicating the start of the time element
HH is the number of complete hours that have passed since midnight as two decimal digits. HH says the full two-digit hour number that has passed since midnight
: ":" (colon) appears literally twice in the string. ":" literally appears between two strings
MM is the number of complete minutes since the start of the hour as two decimal digits. MM represents the full two-digit minutes that have elapsed since the beginning of one hours
SS is the number of complete seconds since the start of the minute as two decimal digits. SS represents the full two-digit number of seconds that have elapsed since the beginning of a minute
. "." (dot) appears literally in the string. "." Literally appear in a string.
SSS is the number of complete milliseconds since the start of the second as three decimal digits.  Both the "." and the milliseconds field may be omitted. SSS represents the full number of milliseconds that have elapsed since the beginning of a second, in three digits. The field can be ignored and not written.
Z is the time zone offset specified as "Z" (in UTC) or either "+" or "-" followed by a when expression hh:mm Z refers specifically to the TIMEZONE offset (especially UT C) or using the "+", "-" followed by a time expression hh:mm. such as +hh:mm

This format includes date-only forms: Such formats can have only dates, allowing only the following formats:

YYYY
yyyy-mm
Yyyy-mm-dd

It also includes time-only forms with a optional time zone offset appended: This format can also be only available in the following formats:

thh:mm
THH:mm:ss
THH:mm:ss.sss

Also included are "Date-times" which may is any combination of the above. You can also include a combination of the dates or times mentioned above.

As you can see, the problem is that the YYYY-MM-DD format is included in the standard, but Safari is not implemented. You can format the various formats for maximum compatibility using the Datejs mentioned above, for example:

var myDate1 = date.parseexact ("29-11-2010", "dd-mm-yyyy");
var myDate2 = date.parseexact ("11-29-2010", "mm-dd-yyyy");
var myDate3 = date.parseexact ("2010-11-29", "yyyy-mm-dd");
var myDate4 = date.parseexact ("2010-29-11", "yyyy-dd-mm");

However, if only a very small amount of use date time, the individual thought that no need to impress wars to use datejs such a library, simple to make a regular match for Safari can recognize the format, as follows:

New Date (' 2011-04-12 '. Replace (/-/g, "/"))

More changes can be made to adjust your code according to your business needs.

Author: cnblogs Many front siege master

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.