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:
- Get the time value from the database and report an error: Java sql. Timestamp cannot be cast to java. lang.Long
- [How to Solve] Failed to convert value of type ‘java.lang.String’ to required type ‘java.util.Date’;
- MySQL from_ Unixtime() and UNIX_ The difference of timestamp() function
- [Solved] java.lang.IllegalArgumentException: Cannot format given Object as a Date
- ValueError: day is out of range for month [How to Solve]
- ValueError: day is out of range for month [How to Solve]
- Using Vue + Axios: axios.post error: request failed with status code 400 is one of the solutions
- C# String was not recognized as a valid DateTime [How to Solve]
- When mysql creates a table, set timestamp DEFAULT NULL error 1067-Invalid default value for’updated_at’