Uniapp IOS uses new date() to report an error

  IOS reports an error using new date()

When developing the app today, I found that the same ‘new date()’ code is OK on Android and will report an error on IOS. Found a plan and recorded it

let stringTime = ‘2021-7-5 17:48:20’
let time1 = new Date().getTime(); // Current timestamp
let time2 = Date.parse(new Date(stringTime)); // Timestamp of the specified time
let time = time1 – time2;
Console.log (time)// Nan

eliminated the problem of time2, but why is there no problem on Android phones. Originally, ‘2021-7-5 17:48:20’ was used on IOS, including ` – ‘, but only the format of ` /’ can be recognized in IOS. Use ‘stringtime. Replace (/ -/g, “/)’ to replace it with ` ‘2021/7/5 17:48:20’, which is no problem

let stringTime = ‘2021-7-5 17:48:20’
let time1 = new Date().getTime(); // Current timestamp
Stringtime = stringtime. Replace (/ -/g, “/”)// this is the IOS pit. Use this to convert it
let time2 = Date.parse(new Date(stringTime)); // Timestamp of the specified time
let time = time1 – time2;
Console.log (time)// 1352063

however, Android has no problem using `/` this. Android doesn’t have to adapt anymore. Just use it directly

Similar Posts: