ios移动端,js时间操作getTime(),getFullYear()等返回显示NaN的解决

在做移动端时间转化为时间戳时,遇到了一个问题,安卓手机上访问时,能拿到时间戳,从而正确转换时间,而在iOS上缺不能正常显示,显示的时间为:NaN-NaN1-NaN 

new Date("2019-04-12 17:30:00".replace(/-/g,'/')).getTime();  // 解决了问题!!

当然getFullYear() ,getMonth(),getDate()需要replace

/**
 * 格式化时间
 * @param {String} str
 * @returns 格式化后的时间
 */
export const TimeData = (str) => {
  if (!str) return ''
  var date = new Date(str.replace(/-/g, '/'))
  var time = new Date().getTime() - date.getTime() // 现在的时间-传入的时间 = 相差的时间(单位 = 毫秒
  if (time < 0) {
    return ''
  } else if ((time / 1000 < 30)) {
    return '刚刚'
  } else if (time / 1000 < 60) {
    return parseInt((time / 1000)) + '秒前'
  } else if ((time / 60000) < 60) {
    return parseInt((time / 60000)) + '分钟前'
  } else if ((time / 3600000) < 24) {
    return parseInt(time / 3600000) + '小时前'
  } else if ((time / 86400000) < 31) {
    return parseInt(time / 86400000) + '天前'
  } else if ((time / 2592000000) < 12) {
    return parseInt(time / 2592000000) + '月前'
  } else {
    return parseInt(time / 31536000000) + '年前'
  }
}

猜你喜欢

转载自blog.csdn.net/qq_37942845/article/details/89249852