一些时间方法

发布于:2025-02-25 ⋅ 阅读:(15) ⋅ 点赞:(0)

1.禁用之前的时间

<el-date-picker
  :picker-options="disableBeforePicker"

disableBeforePicker: {
  disabledDate(time) {
    return time.getTime() < Date.now() - 8.64e7;
  },
},

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}`;
},