1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!-- 日期时间选择器组件   带日周月快捷选项
  前一天是当前时间的前一天的00点到23:59:59
  前七天 和 上一个月同样
  会将初始默认时间(一周前)和改变的时间通过事件‘submitTime’传递给父组件
  
  初始渲染时就将时间传递给父组件:
  ** 
  在父组件中设置
    <TimeShortCuts @submit-time="giveTime"></TimeShortCuts>
   giveTime(val) {
        将中国标准时间转为指定格式(该组件返回的标准时间的格式,所以必须的加这个函数)
        this.beginTime = dayjs(val[0]).format('YYYY-MM-DD HH:mm:ss');
        this.endTime = dayjs(val[1]).format('YYYY-MM-DD HH:mm:ss');
      },
  ***
-->
 
 
<script>
import dayjs from 'dayjs';
import time from '@/utils/time.js'
export default {
  props: {
    beginAndEndTime:{
      type:Array,
      default: ()=>{
        return []
      }
    },
    timeType: {
      type: String,
      default: 'month'
    },
    // 是否设置只读属性
    readOnly: {
      type: Boolean,
      default:false
    }
  },
  emits: ['submitTime'],
  data() {
    return {
      //保存开始和结束时间
      // 随便设置初始值 ,mounted时再设正确的,目的是改变时间了触发change
      time: ['2023-06-01 12:00:00', '2023-06-20 16:00:00'],
      shortcuts: [],
      defaultTime :[
        new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),
      ]
    };
  },
  watch:{
    beginAndEndTime(){
      if(this.beginAndEndTime.lenth!=0){
        this.time = this.beginAndEndTime
      }
    }
  },
  mounted() {
    this.initShortCuts();
    this.initOneWeekAgoTime();
    this.$emit('submitTime', this.time);
  },
  methods: {
    initShortCuts() {
      // this.shortcuts = [
      //   {
      //     text: '前一日',
      //     value: () => {
      //       const start = dayjs().subtract(1,'day').format('YYYY-MM-DD 00:00:00')
      //       const end = dayjs().subtract(1,'day').format('YYYY-MM-DD 23:59:59')
      //       return [start, end];
      //     } 
      //   },
 
      //   {
      //     text: '前7天',
      //     value: () => {
      //       const start = dayjs().subtract(7,'day').format('YYYY-MM-DD 00:00:00');
      //       const end = dayjs().subtract(1,'day').format('YYYY-MM-DD 23:59:59');
      //       return [start, end];
      //     }
      //   },
      //   {
      //     text: '上一月',
      //     value: () => {
      //       const start = dayjs().subtract(1,'month').startOf('month').format('YYYY-MM-DD HH:mm:ss');
      //       const end = dayjs().subtract(1,'month').endOf('month').format('YYYY-MM-DD HH:mm:ss');
      //       return [start, end];
      //     }
      //   }
      // ];
 
 
      this.shortcuts = [
        {
          text: '前一日',
          value: () => {
            if (this.time[0]=='2023-06-01 12:00:00' && this.time[1] == '2023-06-20 16:00:00'){
              const start = dayjs().subtract(1,'day').format('YYYY-MM-DD 00:00:00')
              const end = dayjs().subtract(1,'day').format('YYYY-MM-DD 23:59:59')
              return [start, end];
            }else{
              const start = dayjs(this.time[0]).subtract(1,'day').format('YYYY-MM-DD 00:00:00')
              const end = dayjs(this.time[1]).subtract(1,'day').format('YYYY-MM-DD 23:59:59')
              return [start, end];
            }
          }
        },
 
        {
          text: '前7天',
          value: () => {
            if (this.time[0]=='2023-06-01 12:00:00' && this.time[1] == '2023-06-20 16:00:00'){
              const start = dayjs().subtract(7,'day').format('YYYY-MM-DD 00:00:00');
              const end = dayjs().subtract(1,'day').format('YYYY-MM-DD 23:59:59');
            return [start, end];
            }else{
              const start = dayjs(this.time[0]).subtract(7,'day').format('YYYY-MM-DD 00:00:00')
              const end = dayjs(this.time[1]).subtract(7,'day').format('YYYY-MM-DD 23:59:59')
              return [start, end];
            }
            
          }
        },
        {
          text: '上一月',
          value: () => {
            if (this.time[0]=='2023-06-01 12:00:00' && this.time[1] == '2023-06-20 16:00:00'){
              const start = dayjs().subtract(1,'month').startOf('month').format('YYYY-MM-DD HH:mm:ss');
              const end = dayjs().subtract(1,'month').endOf('month').format('YYYY-MM-DD HH:mm:ss');
              return [start, end];
            }else{
              const start = dayjs(this.time[0]).subtract(1,'month').format('YYYY-MM-DD HH:mm:ss')
              const end = dayjs(this.time[1]).subtract(1,'month').format('YYYY-MM-DD HH:mm:ss')
              return [start, end];
            }
            
          }
        }
      ];
 
    },
 
 
 
 
 
    initOneWeekAgoTime() {
      switch (this.timeType) {
        case 'day':
          this.time[0] = dayjs()
            .subtract(1, 'day')
            .format('YYYY-MM-DD 00:00:00');
          this.time[1] = dayjs().subtract(1, 'day').format('YYYY-MM-DD 23:59:59');
          break;
        case 'week':
          this.time[0] = dayjs()
            .subtract(1, 'week')
            .format('YYYY-MM-DD HH:mm:ss');
          this.time[1] = dayjs().format('YYYY-MM-DD HH:mm:ss');
          break;
        case 'month':
          this.time[0] = dayjs()
            .subtract(1, 'month')
            .format('YYYY-MM-DD HH:mm:ss');
          this.time[1] = dayjs().format('YYYY-MM-DD HH:mm:ss');
          break;
        case 'currentMonth':
        this.time[0] = dayjs().startOf('month').format('YYYY-MM-DD HH:mm:ss')
        this.time[1] = dayjs().subtract(1,'day').format('YYYY-MM-DD 23:59:59')
        // 防止在每月的1号,出现time[0]>time[1]的情况
        if(this.time[0]<this.time[1]){
          break  
          }
        default:
          this.time[0] = dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss');
          this.time[1] = dayjs().format('YYYY-MM-DD HH:mm:ss');
      }
    },
    judgeDateValid(date) {
          return time.judgeDateValid(date)
        }
  }
};
</script>
 
<template>
  <div class="block">
    <span class="demonstration">起止时间:</span>
    <div class="pick-date">
    <el-date-picker
      v-model="time"
      type="datetimerange"
      :shortcuts="shortcuts"
      range-separator="~"
      start-placeholder="开始时间"
      end-placeholder="结束时间"
      value-format="YYYY-MM-DD HH:mm:ss"
      :disabled-date="judgeDateValid"
      @change="$emit('submitTime', time)"
      :default-time = "defaultTime"
      :readonly="readOnly"
    />
      
 
  </div>
  </div>
</template>
 
<style scoped>
.block {
  display: flex;
  width: 500px;
}
.demonstration {
  color: #333333;
  font-weight: bold;
  font-size: 14px;
}
 
 
</style>