当前位置: 首页 > Web前端 > vue.js

时间戳转换年月日-周-昨天

时间:2023-03-31 21:18:22 vue.js

借助dayjs(处理时间和日期的轻量级JavaScript库)API中文文档GitHub地址API转载安装:npm:npminstalldayjs--saveimportdayjsfrom'dayjs'//orCommonJS//vardayjs=require('dayjs');dayjs().format();//2020-01-06T10:15:01+08:00CDN:实现vardayjs=require('dayjs');//当前年份的开始//console.log(dayjs().startOf('year').$d.valueOf());letyearStar=dayjs().startOf('year').$d.valueOf();//当月的开始//console.log(dayjs().startOf('month').$d.valueOf());letmonthStar=dayjs().startOf('month').$d.valueOf();//最近一周开始//console.log(dayjs().startOf('week').$d.valueOf());letweekStar=dayjs().startOf('week').$d.valueOf();//当前一天的开始//console.log(dayjs().startOf('day').$d.valueOf());让todayStar=dayjs().startOf('day').$d.valueOf();//昨天的开始letyestodayStar=dayjs().startOf('day').$d.valueOf()-1000*60*60*24;letcurrenttime;letss=1577145600000;//super显示今年的年份if(ss10?month=month:month='0'+monthdate>10?date=date:date='0'+datecurrenttime=`${dayjs(ss).year()}-${month}-${date}`}//今年不在这个月if(ss>yearStar&&ss10?month=month:month='0'+monthdate>10?date=date:date='0'+datecurrenttime=`${dayjs(ss).year()}-${month}-${date}`}//这个月不是本周if(ss>monthStar&&ss10?month=month:month='0'+monthdate>10?日期=date:date='0'+datecurrenttime=`${month}-${date}`}//这周不是昨天if(ss>weekStar&&ss10?hour=hour:hour='0'+hourminute>10?minute=minute:minute='0'+minutecurrenttime=`week${weekValue}-${hour}:${minute}`}//昨天的if(ss>yetodayStar&&ss10?hour=hour:hour='0'+hourminute>10?minute=minute:minute='0'+minutecurrenttime=`yesterday-${hour}:${minute}`}//if(ss>todayStar){//小时+分钟letss=1577145600000;Console.log('今天');让hour=dayjs(ss).hour()让minute=dayjs(ss).minute()hour>10?hour=hour:hour='0'+hourminute>10?minute=minute:minute='0'+minutecurrenttime=`${hour}:${minute}`}console.log(currenttime);