js对比选择的日期是否大于今天,js判断时间选择。

// 时间选择不能超过当日
dateNow() {
  // 获取当前日期
  var today = new Date()
  today.setHours(0, 0, 0, 0) // 将时、分、秒、毫秒设置为0,只比较日期

   // 获取选择的时间
   // 获取用户点击后选择的时间并转换为 Mon Jul 24 2023 08:00:00 GMT+0800 (中国标准时间) 格式
   var selectedDate = new Date(this.form.time) 
   selectedDate.setHours(0, 0, 0, 0)

   // 比较选择的时间和今天的时间
   if (selectedDate.getTime() > today.getTime()) { // getTime() 方法返回一个时间的格林威治时间数值
     console.log(selectedDate.getTime()) // // 打印 1690128000000 
     this.form.time = ''
     return this.$message.error('选择的时间不能大于今天')
   }
},

猜你喜欢

转载自blog.csdn.net/guojixin12/article/details/131892749