ios safari 中时间 Invalid Date及Nah问题

ios safari 中时间 Invalid Date及Nah问题,第1张

new Date(date)在ie、fierfox、chrome,以及移动安卓端都能正常运行,但是唯有在Safariios中显示为 Invalid Date,日期显Nah-Nah-Nah
原因是ios和safari不支持“YYYY-MM-DD”的时间格式,
除此之外safari不支持的时间格式还有:

YYYY-M-DD ("2017-1-07")
YYYY-MM-DD hh:mm:ss ("2017-1-07 00:00:00")

vue项目实现日期显示“刚刚”、“1分钟前”、“1天前”、“2022-02-23”等

method()如下:

    //时间显示
    formatDatenn(date) {
      return new Date(date.replace(/-/g, "/"))
    },
    showtime(time) {
      let that = this
      let newtime = that.formatDatenn(time)
      let date = new Date(newtime)
      let diff = (new Date().getTime() - date.getTime()) / 1000;
      let dayDiff = Math.floor(diff / 86400);//一天
      const formatDate = function (date) {
        let today = new Date(date);
        let year = today.getFullYear();
        let month = ("0" + (today.getMonth() + 1)).slice(-2);
        let day = ("0" + today.getDate()).slice(-2);
        // let hour = today.getHours();
        // let minute = today.getMinutes();
        // let second = today.getSeconds();
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
      };

      if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) {
        return formatDate(date);
      }
      return (
        (dayDiff === 0 &&
          ((diff < 60 && "刚刚") ||
            (diff < 120 && "1分钟前") ||
            (diff < 3600 && Math.floor(diff / 60) + "分钟前") ||
            (diff < 7200 && "1小时前") ||
            (diff < 86400 && Math.floor(diff / 3600) + "小时前"))) ||
        (dayDiff === 1 && "昨天") ||
        (dayDiff < 7 && dayDiff + "天前") ||
        (dayDiff < 31 && Math.ceil(dayDiff / 7) + "周前")
      );
    }

 vue中使用

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993170.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存