前端时间戳处理去年昨天当天基于dayjs

方法内代码:参数date为时间戳

复制代码
 let that = this;
    if (!date) {
      return;
    }
    date = date * 1000
    const IN_DAY = 1000 * 60 * 60 * 24 * 1; //1天
    const Yesterday = 1000 * 60 * 60 * 24 * 2; //昨天

    let Y = dayjs(date).year(),
      M = dayjs(date).month(),
      D = dayjs(date).date(),
      // W = dayjs(date).day(),
      W = dayjs(date).format('ddd'),
      H = dayjs(date).hour(),
      Min = dayjs(date).format('mm'),
      nowY = dayjs().year(),
      nowM = dayjs().month(),
      nowD = dayjs().date(),
      timeStr = '';

    if (Y < nowY) {
      // 去年
      timeStr = `${y}/${M}/${D} `
    } else {
      if (Y === nowY && M === nowM) {
        // 昨天
        if ((nowD - D) === 1) {
          timeStr = '昨天 '
        } else if (nowD === D) {
          // 当天
          timeStr = ''
        } else {
          // 一周内
          timeStr = W + ' '
        }
      } else {
        timeStr = `${M}/${D} `
      }
    }
    timeStr += `${H}:${Min}`
    return timeStr
复制代码

 

npm地址:https://www.npmjs.com/package/dayjs

npm安装:

1
npm install dayjs --save

  

posted @   君宁天下  阅读(1985)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
野生程序员真的是太难了,一刻也不敢停止学习
点击右上角即可分享
微信分享提示