Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Date Ionic 3 in IOS

Invalid Date Error in ionic 3 in IOS only. (Working fine in Android).

var f = data.posted_date +' '+ data.posted_time;
      var d = new Date(); // working Fine
      var b = new Date(f); // b becomes invalid Date

its working fine in android but not working in IOS.

Complete Function:

  getProperTime() {
    this.dailyDiary.forEach(element => {
      var d = new Date(element.posted_date + " " + element.posted_time);
      var hours: any = d.getHours();
      var minutes: any = d.getMinutes();
      var ampm = hours >= 12 ? "pm" : "am";
      hours = hours % 12;
      hours = hours ? hours : 12; // the hour '0' should be '12'
      minutes = minutes < 10 ? "0" + minutes : minutes;
      var strTime = hours + ":" + minutes + " " + ampm;
      console.log("this is the getProperTime function output: " + strTime);
      element.posted_time = strTime;
    });
  }

This function shows as result:

12:NaN am

like image 574
Hanzala Raza Avatar asked May 13 '26 05:05

Hanzala Raza


1 Answers

It looks like Date Format Issue in Safari View.

var d = new Date("2011-02-07");

Above Mentioned Format is Not supported in Internet Explorer and Safari.

these two browsers surprisingly do not support the date format “yyyy-mm-dd” and therefore fail. Supported across all browsers and would advise sticking to one of these to avoid errors:

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
like image 120
Najam Us Saqib Avatar answered May 15 '26 20:05

Najam Us Saqib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!