实时获取当前时间和一个月之前的时间

使用npm 下载 moment日期处理库。

npm install moment --save 

当前时间:

time1:moment((new Date()).format("YYYY-MM-DD hh:mm:ss"))

30天之前的时间:

moment((new Date().getTime() - 86400*30*1000).format("YYYY-MM-DD hh:mm:ss"))

new Date().getTime()  是1970年01月01日8点中以来的毫秒数.

如何校验两个日期是否超过7天时间

这两个,一个是七天前的时间,一个是当前时间。

startTime:moment(new Date().getTime()-86400*7*1000).format("YYYY-MM-DD hh:mm:ss"), //开始时间
endTime:moment(new Date()).format("YYYY-MM-DD hh:mm:ss"), // 结束时间

首先判断两个时间不能为空,再把它们的时间转换成毫秒值进行比较,看是否大于七天的毫秒值。

if(this.endTime == "" || this.startTime ==""){
  return this.$Message.error("时间不能为空!")
}
if(this.endTime.getTime() - this.startTime.getTime() > 604800000){
  return this.$Message.error("开始时间和结束时间不能超过一个星期!!")
}

最后再进行相应的操作。

猜你喜欢

转载自blog.csdn.net/weixin_48674314/article/details/119810275