小程序格式化时间在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)
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值