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

时间格式的转换与使用

时间:2023-03-31 22:45:56 vue.js

dateUtil.js/***格式化日期*@param{*}timestamp待转换时间戳*@param{*}formate格式同时支持yyyy-MM-ddHH:mm:ss和yyyy-MM-ddKind*@param{*}separatorseparator*/consttimestampToTime=(timestamp,formate,separator)=>{letresult=timestamp//字符串完整返回if(timestamp&&typeoftimestamp!==''string''){if(timestamp.toString().length===10){timestamp=timestamp*1000////如果时间戳为10位,则需要*1000,如果时间戳为13位,则无需乘以1000}separator=分隔符||''-''letdate=newDate(timestamp)letyear=date.getFullYear()+separatorletmonth=(date.getMonth()+1<10?''0''+(date.getMonth()+1):date.getMonth()+1)+分隔符//让nowDate=date.getDate()+'''';让nowDate=date.getDate()<10?''0''+date.getDate():date.getDate()lethour=date.getHours()hour=hour<10?''0''+hour:hourhour=hour+'':''letminutes=date.getMinutes()minutes=minutes<10?''0''+minutes:minutesminutes=minutes+'':''letsecond=date.getSeconds()second=second<10?''0''+second:secondif(formate===''yyyy-MM-dd''){result=result=year+month+nowDatereturnresult}nowDate+=''''result=year+month+nowDate+hour+minutes+second}returnresult}计算从现在到目标日期还剩多少天引入importdateUtilfrom'@/util/dateUtil'validEndDate(endTime){//'目标时间:'2020-5-4''后跟23:59:59得到最终的毫秒数constendTimeStr=`${endTime}23:59:59`constdefaultFormat=''yyyy-MM-dd''consttimed=newDate(.getTime()conststartTimeStamp=dateUtil.timestampToTime(timed,defaultFormat)//获取当前'2020-5-1'conststartTimeStr=`${startTimeStamp}00:00:00`//后面是00:00:00conststartTime=newDate(startTimeStr).getTime()//从0点开始的毫秒数constendTimeCompu=newDate(endTimeStr).getTime()//获取最后的毫秒数consttimeDiff=Math.abs(endTimeCompu-startTime)//数数计算毫秒差constday=timeDiff/(24*60*60*1000)//转换为天this.toDate=Math.ceil(day)},