Invalid safari date

alert(new Date('2010-11-29')); 

chrome, ff has no problem with this, but safari shouts out "invalid date". What for?

edit: ok, according to the comments below, I used string parsing and tried this:

 alert(new Date('11-29-2010')); //doesn't work in safari alert(new Date('29-11-2010')); //doesn't work in safari alert(new Date('2010-29-11')); //doesn't work in safari 

edit March 22, 2018 : It looks like people are still landing here - Today I would use moment or date-fns and get it over with. Date-fns is also very painless and light.

+114
javascript date safari
Nov 30 '10 at 5:58
source share
15 answers

The yyyy-MM-dd template is not an officially supported format for the Date constructor. Firefox seems to support this, but don’t expect other browsers to do the same.

Here are some of the supported strings taken from this site :

  • MM-dd-yyyy
  • dd yyyy / MM /
  • MM / dd / yyyy
  • MMMM dd, yyyy
  • Mmm dd, yyyy

DateJS seems like a good library for parsing custom date formats.

Edit : just checked the ECMA-262 standard . Quote from section 15.9.1.15:

Date Time String Format

ECMAScript defines the string exchange format for date and time based on the simplified extended format ISO 8601. The format is as follows: YYYY-MM-DDHH: mm: ss.sssZ Where are the following fields:

  • YYYY - decimal digits of the year in the Gregorian calendar.
  • "-" (hyphon) appears literally twice in a row.
  • MM - month of the year from 01 (January) to 12 (December).
  • DD is the day of the month from 01 to 31.
  • The letter "T" appears literally in the line to indicate the start of a time element.
  • HH - the number of full hours that have passed since midnight in the form of two decimal digits.
  • ":" (colon) appears literally twice in a line.
  • mm - the number of full minutes since the beginning of the hour in the form of two decimal digits.
  • ss - the number of full seconds from the beginning of the minute in the form of two decimal digits.
  • "" (dot) appears literally on the line.
  • sss is the number of full milliseconds since the beginning of a second in the form of three decimal digits. Both are "." and the millisecond field may be omitted.
  • Z is the time zone offset indicated as β€œZ” (for UTC) or β€œ+” or β€œ-”, followed by the time expression hh: mm

This format includes date-only forms:

  • Yyyy
  • YYYY-MM
  • YYYY-MM-DD

It also includes only temporary forms with the addition of an optional time zone offset:

  • THH: mm
  • THH: mm: ss
  • THH: mm: ss.sss

Also included are "date-time", which can be any combination of the above.

It appears that YYYY-MM-DD is included in the standard, but for some reason Safari does not support it.

Update : after viewing the datejs documentation and its use, your problem should be solved with the following code:

 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"); 
+134
Nov 30 '10 at 6:03
source share

For me, implementing a new library is simply because Safari cannot do it right, too much, and the regex is full. Here is the oneliner :

 console.log (new Date('2011-04-12'.replace(/-/g, "/"))); 
+179
Apr 13 '11 at 9:01
source share

I ran into a similar problem. Date.Parse("DATESTRING") worked on Chrome (version 59.0.3071.115), but not on Safari (version 10.1.1 (11603.2.5))

Safari:

 Date.parse("2017-01-22 11:57:00") NaN 

Chromium:

 Date.parse("2017-01-22 11:57:00") 1485115020000 

The solution that worked for me was to replace the space in dateString with "T" . (example: dateString.replace(//g,"T") )

Safari:

 Date.parse("2017-01-22T11:57:00") 1485086220000 

Chromium:

 Date.parse("2017-01-22T11:57:00") 1485115020000 

Please note that the response from the Safari browser is 8 hours (28,800,000 ms) less than the response from the Chrome browser, because Safari returned the response in local TZ (which is 8 hours later UTC)

To get both times in the same TZ

Safari:

 Date.parse("2017-01-22T11:57:00Z") 1485086220000 

Chromium:

 Date.parse("2017-01-22T11:57:00Z") 1485086220000 
+25
Jul 17 '17 at 19:31
source share

I use the moment to solve the problem. for example

 var startDate = moment('2015-07-06 08:00', 'YYYY-MM-DD HH:mm').toDate(); 
+8
Jul 6 '15 at 7:21
source share

For the solution to work in most browsers, you must create a date object with this format.

 (year, month, date, hours, minutes, seconds, ms) 

eg:

 dateObj = new Date(2014, 6, 25); //UTC time / Months are mapped from 0 to 11 alert(dateObj.getTime()); //gives back timestamp in ms 

works great with IE, FF, Chrome and Safari. Even older versions.

IE Dev Center: Date Object (JavaScript)

Mozilla Developer Network: Date

+7
Jul 25 '14 at 10:48
source share

convert string to Date fromat (you must know server timezone)

 new Date('2015-06-16 11:00:00'.replace(/\s+/g, 'T').concat('.000+08:00')).getTime() 

where +08: 00 = timeZone from the server

+6
Jun 16 '15 at 11:14
source share

I had the same problem. Then I used moment.Js . The problem is gone.

When creating a moment from a string, we first check whether the string matches the known ISO 8601 formats, then returns to the new date (string) if a known format is not found.

Warning. Browser support for parsing strings is inconsistent. Because there are no specifications according to which formats should be supported, what works in some browsers will not work in other browsers.

For consistent results parsing everything except ISO 8601 strings, you should use String + Format.

eg.

 var date= moment(String); 
+4
Dec 11 '14 at 10:47
source share

Although you can hope that browsers will support ISO 8601 (or subsets of them for date only), this is not the case. All browsers that I know about (at least in the American and English places that I use) can parse the terrible US MM/DD/YYYY format.

If you already have date parts, you can use Date.UTC () instead. If you do not, but you must use the YYYY-MM-DD format, I suggest using a regular expression to parse the fragments that you know, and then pass them to Date.UTC() .

+3
Nov 30 '10 at 6:08
source share

Use the following format, it will work in all browsers

 var year = 2016; var month = 02; // month varies from 0-11 (Jan-Dec) var day = 23; month = month<10?"0"+month:month; // to ensure YYYY-MM-DD format day = day<10?"0"+day:day; dateObj = new Date(year+"-"+month+"-"+day); alert(dateObj); 

// Your conclusion will look like this: "Wed 03.23.2006 00:00:00 GMT + 0530 (IST)"

// Note that this will be in the current time zone in this case, indicated by IST, to convert to UTC time zone, you can enable

 alert(dateObj.toUTCSting); 

// Your result will now like "Tue, 22 Mar 2016 18:30:00 GMT"

Note that dateObj now shows GMT time, also note that the date and time have been changed accordingly.

The "toUTCSting" function receives the corresponding time in the hryvnia meridian. This is achieved by setting the time difference between your current time zone and the Greenwich Meridian time zone.

In the above case, the time to conversion was 00:00 hours and minutes on March 23, 2016. And after switching from GMT + 0530 (IST) hours to GMT (it basically subtracts 5:30 hours from this timestamp in this case), the time is reflected at 18.30 hours on March 22, 2016 (exactly 5.30 hours for the first time).

To convert any date object to a timestamp, you can use

 alert(dateObj.getTime()); 

// the output will be similar to this "1458671400000"

This will give you a unique timestamp.

+1
Feb 09 '16 at 10:10
source share

What about hijack Date with fix-date ? Without dependencies, min + gzip = 280 B

+1
Jul 14 '17 at 4:46 on
source share

The best way to do this is to use the following format:

 new Date(year, month, day, hours, minutes, seconds, milliseconds) var d = new Date(2018, 11, 24, 10, 33, 30, 0); 

This is supported in all browsers and will not cause you any problems. Please note that months are written from 0 to 11.

+1
Jun 01 '18 at 8:04 on
source share

The same problem encountered in Safari was resolved by pasting this into a web page

  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script> 

Hope this will work and your case too

thank

0
Feb 08 '18 at 12:07
source share

As @nizantz mentioned earlier, using Date.parse () didn't help me in Safari. After a little research, I found out that the lastDateModified property for the File object is deprecated and is no longer supported by Safari. Using the lastModified property of the File object solved my problems. Of course, I do not like it when bad information is found on the Internet.

Thanks to everyone who contributed to this post that helped me follow the path I need to learn about my problem. If not for this information, I probably would not have figured out my main problem. Maybe this will help someone else in my similar situation.

0
Feb 28 '19 at 14:03
source share

I am also facing the same issue in Safari browser

 var date = new Date("2011-02-07"); console.log(date) // IE you get 'NaN returned and in Safari you get 'Invalid Date 

Here is the solution:

 var d = new Date(2011, 01, 07); // yyyy, mm-1, dd var d = new Date(2011, 01, 07, 11, 05, 00); // yyyy, mm-1, dd, hh, mm, ss var d = new Date("02/07/2011"); // "mm/dd/yyyy" var d = new Date("02/07/2011 11:05:00"); // "mm/dd/yyyy hh:mm:ss" var d = new Date(1297076700000); // milliseconds var d = new Date("Mon Feb 07 2011 11:05:00 GMT"); // ""Day Mon dd yyyy hh:mm:ss GMT/UTC 
0
May 23 '19 at 9:40
source share

use the format 'mm / dd / yyyy'. For example: - new date ('02 / 28/2015 '). It works well in all browsers.

-one
Feb 25 '15 at 19:39
source share



All Articles