vue2 时间戳 转换

vue2 时间戳 转换,第1张

//   时间戳
     timeFilter(val){
      //   console.log(val);
      let result;
      let timePublish = new Date(val); // 传入的时间 
      let timeNow = new Date(); // 现在的时间 
      let minute = 1000 * 60;
      let hour = minute * 60;
      let day = hour * 24;
      let month = day * 30;
      let year = month * 12;
      let diffValue = timeNow - timePublish; // 时间相差的值,是毫秒 
      let diffMonth = diffValue / month;
      let diffWeek = diffValue / (7 * day);
      let diffDay = diffValue / day;
      let diffHour = diffValue / hour;
      let diffMinute = diffValue / minute;
      let diffYear = diffValue / year;
      if (diffValue < minute) {
        result = "刚刚";
      } else if (diffYear > 1) {
        result = parseInt(diffYear) + "年前";
      } else if (diffMonth > 1) {
        result = parseInt(diffMonth) + "月前";
      } else if (diffWeek > 1) {
        result = parseInt(diffWeek) + "周前";
      } else if (diffDay > 1) {
        result = parseInt(diffDay) + "天前";
      } else if (diffHour > 1) {
        result = parseInt(diffHour) + "小时前";
      } else if (diffMinute > 1) {
        result = parseInt(diffMinute) + "分钟前";
      } 
      return result;
    }

​或者是下载时间戳的一个插件  http://momentjs.cn/

npm install moment --save 

npmyarn add moment # Yarn

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存