小程序格式化时间在IOS系统中显示NaN

2020-05-25T23:50:52.000+0000格式兼容IOS

ios系统不支持2018-03-29格式的时间,只识别2018/03/09这种格式。

export function timeFormatIos(timedata) {
  const dateStr = timedata.substr(0, 19);
  const formatTime = dateStr.replace(/-/g, "/").replace("T", " ");
  const date = new Date(formatTime);
  const Year = date.getFullYear();
  const Month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  const d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  const Hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  const Minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  const Seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  const over_time = Year + "/" + Month + "/" + d + " " + Hours + ":" + Minutes + ":" + Seconds;
  //至此以上是将时间2020-03-18T01:57:23.000+0000转为正常时间格式,以下为将时间进行增加8小时解决时区差异
  const time = new Date(Date.parse(over_time));
  time.setTime(time.setHours(time.getHours() + 8));
  const Y = time.getFullYear() + '/';
  const M = add0(time.getMonth() + 1) + '/';
  const D = add0(time.getDate()) + ' ';
  const h = add0(time.getHours()) + ':';
  const m = add0(time.getMinutes()) + ':';
  const s = add0(time.getSeconds());
  return (Y + M + D + " " + h + m + s)
}

版权声明:本文为weixin_44672601原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。