一些时间方法

1.禁用之前的时间

2.选择开始时间之后,结束时间为开始时间之后的120分钟,他们的格式是yyyy-MM-dd HH:mm:ss

dataChange(value){
  if(value&&!this.endTime){
    const startDate = new Date(this.startTime.replace(/-/g, '/'));
    startDate.setMinutes(startDate.getMinutes() + 120);
    const year = startDate.getFullYear();
    const month = String(startDate.getMonth() + 1).padStart(2, '0');
    const day = String(startDate.getDate()).padStart(2, '0');
    const hours = String(startDate.getHours()).padStart(2, '0');
    const minutes = String(startDate.getMinutes()).padStart(2, '0');
    const seconds = String(startDate.getSeconds()).padStart(2, '0');
    this.endTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
},

3.比较大小,获取当前时间

time(value, time) {
  const valueDate = new Date(value); // 将 value 转换为 Date 对象
  const timeDate = new Date(time); // 将 time 转换为 Date 对象
  return valueDate > timeDate; // 比较时间戳
},
isDate(value, time) {
  const valueDate = new Date(value); // 将 value 转换为 Date 对象
  const timeDate = new Date(time); // 将 time 转换为 Date 对象
  return valueDate >= timeDate; // 比较时间戳
},
getCurrentTime() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,需要 +1
  const day = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');
  const seconds = String(now.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
},

你可能感兴趣的:(vue.js,前端,javascript)