ios端显示NaN的原因是:ios设备不支持new Date(time)的这个time格式为,即:yyyy-mm-dd。

wxs文件不支持new Date,所以我们需要使用getDate

//timestamp   时间戳
//option      格式(年月日  就输入YY-MM-DD   时分  就输入 hh-mm)
//
function formatDate(timestamp, option) {

  var times = timestamp.replace("-", "/").replace("-", "/")
  console.log(times)
  var date = getDate(times);
  var year = date.getFullYear();
  var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  var over_time = year + "/" + month + "/" + day + " " + hours + ":" + minutes + ":" + seconds
  //***至此以上是将时间2020-03-18T01:57:23.000+0000转为正常时间格式,以下为将时间进行增加8小时解决时区差异的操作***
  var time = getDate(Date.parse(over_time));
  time.setTime(time.setHours(time.getHours() + 8));

  //获取 年月日
  if (option == 'YY-MM-DD') return " " + year + "-" + month + "-" + day;

  //获取年月
  if (option == 'YY-MM') return " " + year + "-" + month;

  //获取年
  if (option == 'YY') return " " + year;

  //获取月
  if (option == 'MM') return " " + month;

  //获取日
  if (option == 'DD') return " " + day;

  //获取昨天
  if (option == 'yesterday') return " " + day - 1;

  //获取时分秒
  if (option == 'hh-mm-ss') return " " + hours + ":" + minutes + ":" + seconds;

  //获取时分
  if (option == 'hh-mm') return " " + hours + ":" + minutes;

  //获取分秒
  if (option == 'mm-ss') return minutes + ":" + seconds;

  //获取分
  if (option == 'mm') return minutes;

  //获取秒
  if (option == 'ss') return second;

  //默认时分秒年月日
  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ":" + seconds;
}


module.exports = {
  formatDate: formatDate
}