Reputation: 149
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
Upvotes: 3
Views: 1441
Reputation: 3404
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
Upvotes: 3