Javascript works in all browsers except iPhone / iPod Touch

I have code for working with dates in javascript. This works in IE, FF, Safari (desktop versions of win and mac), Chrome, Opera. In iPhone Safari (mobile safari), I get the answer "wrong date".

Date Management Code

  function fixDateFormat (dateText) {
     var isoExp = / ^ \ s * (\ d {4}) - (\ d \ d) - (\ d \ d) \ s * $ /,
         newDate = new Date (NaN), month,
         parts = isoExp.exec (dateText);

     if (parts) {
       month = + parts [2];
       newDate.setFullYear (parts [1], month - 1, parts [3]);
       if (month! = newDate.getMonth () + 1) {
         newDate.setTime (NaN);
       } else {
         newDate.setHours (0, 0, 0, 0);
       }
     }
     return newDate;
     }

If dateFormat is sent to this function as Ymd (although I understand that this function will deal with many formats).

+4
source share
2 answers

I solved it by passing "milliseconds from an era" instead of this formatted date.

0
source

There should be some error in Mobile Safari, since I have this problem, it works everywhere except for the iOS device. It does not correctly handle the correct ISO date and time (for example, "2011-10-09T14: 00: 00.0000000 + 01: 00").

The problem is using the UNIX timestamp, although it works with the new Date () method. setTime (); time is converted to UTC, so if your application does not handle UTC offsets, it will show the wrong time. In particular, if the date-time refers to a date in the future or during a period when the daylight saving time has changed, the time in the time stamp of the era will be incorrect. This is why timestamps are not used to store date and time values.

The only workaround I could fix was to split the date object into a JSON object containing its properties, and then restore them on the client back to the Date () object.

0
source

Source: https://habr.com/ru/post/1303733/


All Articles