Live2D

获取当前年月日格式带零的方法

一行代码搞定:

new Date().getFullYear() + (new Date().getMonth() + 1).toString().padStart(2, "0") + new Date().getDate().toString().padStart(2, "0")
注意点:获取当前月份后要加一!
 
 
补充获取近一周的年月日:
 
getDay(day) {
  let today = new Date();
  let targetday=today.getTime() + 1000*60*60*24*day;
  today.setTime(targetday);
  let tYear = today.getFullYear();
  let tMonth = today.getMonth();
  let tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return tYear+tMonth+tDate;
},
doHandleMonth(month){
  let m = month;
  if(month.toString().length == 1){
    m = "0" + month;
  }
  return m;
},
const dateList=[]
for(let i=0;i<-7;i--){
dateList.push(getDay(i))
}
 
根据循环生成自己想要的数据!
 
 
 
 
 
 
 
比较当前时间是否在 格式为(2021-08-12 00:00:00)的时间段中
 
 
两行行代码搞定:(比较时间戳)
const nowTimeStamp=Date.parse(new Date())
{nowTimeStamp-Date.parse(new Date(record.startTimeStr))<0?"活动未开始":(nowTimeStamp-Date.parse(new Date(record.endTimeStr))>0?"活动已结束":"活动进行中")}
posted @ 2021-08-05 10:49  喻佳文  阅读(769)  评论(3编辑  收藏  举报