ios safari 中时间 Invalid Date及Nah问题

new Date(date)在ie、fierfox、chrome,以及移动安卓端都能正常运行,但是唯有在Safari和ios中显示为 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中使用

你可能感兴趣的:(JavaScript,vue,safari,ios,前端)