[Applet] Use uni-app to build applet environment --- Time format problem new Date (str) IOS system is not compatible with Android system

Today, I made a requirement to display the time returned from the background in the list. It is no problem to use new Date (str) to display on the WeChat developer tool, and then display NAN on the iOS system.

The reason is that the IOS system only recognizes "/" does not recognize "-". 

There are generally three types of time returned from the background, time, time, and character string. Let's study how to deal with these three types to be compatible with Android system and IOS system.

Copy code
formatTime(d) {
  var year = d.getFullYear();
  var month = d.getMonth() + 1;
  var date = d.getDate();
  var hour = d.getHours();
  var minute = d.getMinutes();
  var second = d.getSeconds();
  return year + "/" + (D[month] || month) + "/" + (D[date] || date) + " " + (D[hour] || hour) + ":" + (D[minute] || minute) + ":" + (D[second] || second);//必须是‘/’格式
}
Copy code

 

1. String type 2019-8-14 10:03:45  

Copy code
var date = "2019-8-14 10:03:45" 

// Incompatible code 
var newDate = new Date (date); 

// Solved 
var newDate = new Date (date.replace (/-/ g, '/' ));


Copy code

 

2. Timestamp type 1565776169000 (milliseconds) Last display 2019/08/14 17:49:29

var date = 1565776169000; // Must be a numeric type, must be 13 digits 
var newDate = new Date (date); 
var newDateTime = dateFormater.formatTime (newDate) // 2019/08/14 17:49:29

 

3. Time type 2019-08-09T18: 23: 27.000 + 0800

var date = '2019-08-09T18:23:27.000+0800';
var data = date.substr(0, 19); //'2019-08-09T18:23:27'
var newDate = new Date(data.replace(/T/g, ' ').replace(/-/g, '/'));
                                

 

Fourth, the ultimate processing time plugin  moment.js

Guess you like

Origin www.cnblogs.com/websmile/p/11943538.html