Mpvue WeChat applet: time conversion Android and iOS compatibility issues

Android and iOS have different delimiters for time resolution. 1.Android uses’ – ‘to partition. 2.IOS uses’/’ to partition

If you use new Date(‘2019-06-17’) in iOS, you will get null. If you do not know ‘-‘ in iOS, you need to replace it with ‘/’. String methods include the str.replace() method.

String STR. Replace () 1. STR. Replace (‘ string to be replaced ‘, ‘new string’) // This method can only replace the first character in a string; 2. Str. Replace (/ string to be replaced /g, “new string “) // Use a regular expression to replace all characters in a string (add a backslash if the characters are ‘-‘, ‘/’, etc.) .

FormatDate (Time) {FormatDate (Time) {FormatDate (Time) {FormatDate (Time) {FormatDate (Time); let result = ''; // if(res.platform == "devtools"){// if(res.platform == "devtools"){// if(res.platform == "devtools"){// if(res.platform == "devtools"){ The console. The log (' I'm a PC); newTime = time; }else if(res.platform == "iOS "){console.log(' I am iOS '); newTime = time.replace(/-/g, '/'); }else if(res.platform == "Android "){console.log(' I'm Android '); newTime = time; } let lr = new Date(newTime); Let now = new Date(); // let dt = now -lr; let second = dt / 1000; If (second < 3600) {result = parseInt(s / 60) + 'min '; } else if(second < 86400) {result = parseInt(s / 60/60) + 'hour '; } else if(second <604800) {result = parseInt(s / 60/60/24) + 'day '; } else if(second <31104000) {result = parseInt(s / 60/60/24/30) + 'month '; } else if(second <311040000) {result = parseInt(s / 60/60/24/30/12) + 'year '; }}, fail(err){console.log(' device system information failed ', err)},}) return result}

This is a time conversion problem I encountered in the use of MPVue development WeChat small program, I hope to help NI; If there is anything, we can talk more later.