-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate-picker.js
456 lines (449 loc) · 14.8 KB
/
date-picker.js
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
function toDate(date) {
if (!date) {
date = new Date();
}
if (Object.prototype.toString.call(date) == '[object Date]') {
return date
} else if (Object.prototype.toString.call(date) == '[object String]') {
if (!isNaN(date)) {
return new Date(Number(date))
} else if (date.includes('T')) {
return new Date(date)
} else {
return new Date(date.replace(/-/ig, '/'))
}
} else if (Object.prototype.toString.call(date) == "[object Number]") {
return new Date(date)
}
}
function Appendzero(obj) {
if (obj < 10) {
return '0' + '' + obj;
}
else {
return obj;
}
}
function formatDate(date, format = "") {
date = toDate(date);
if (!format) {
format = 'yyyy-MM-dd';
}
if (format == 'number') {
return date.getTime();
}
const week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', '日', '一', '二', '三', '四', '五', '六'];
return format.replace(/yyyy|yy|MM|M|dd|d|hh|mm|ss|星期|周|www|week/g, function (a) {
switch (a) {
case 'yyyy':
return date.getFullYear();
case 'yy':
return (date.getFullYear() + '').slice(2);
case 'MM':
return Appendzero(date.getMonth() + 1);
case 'M':
return date.getMonth() + 1;
case 'dd':
return Appendzero(date.getDate());
case 'd':
return date.getDate();
case 'hh':
return Appendzero(date.getHours());
case 'mm':
return Appendzero(date.getMinutes());
case 'ss':
return Appendzero(date.getSeconds());
case '星期':
return '星期' + week[date.getDay() + 7];
case '周':
return '周' + week[date.getDay() + 7];
case 'week':
return week[date.getDay()];
case 'www':
return week[date.getDay()].slice(0, 3);
}
})
}
Component({
properties: {
//单选时间 传值 也是第一个值
value: {
type: [String, Number, Object],
value: ""
},
//时间区间范围设置,
// 当为string strat&&end start||end 如 2016-02-02&&2016-05-02 && 两个时间内可选,||两个时间外可选
// 当为string strat> 或<start 前后都可以 如 2016-02-02> >2016-02-02, 表示大于给定日期之后的日期可选,反之小于也是同理
// 当为Array ['2016-02-02','2016-02-01'] 指定时间不可选
// limit="2023-02-07&&2023-02-18" limit="[2023-02-07,2023-02-18]" limit="30"
limit: {
type: [String, Number, Array],
value: "yyyy-MM-dd"
},
//多选时间 第二个值
end: {
type: [String, Number, Object],
value: ""
},
// 日历标题 是否显示时分秒
time: {
type: [Boolean, String],
value: false
},
// 是否可以选择两个时间,开始时间,与结果时间
more: {
type: Boolean,
value: false
},
// 是否可以选择两个时间,开始时间,与结果时间
button: {
type: [Boolean, String],
value: false
},
// timestamp或number 时间戳,string:'yyyy-MM-dd hh:mm:ss' 返回的绑定值格式
format: {
type: String,
value: "yyyy-MM-dd"
},
//错误提示语
message: {
type: [Boolean, String],
value: ""
},
// 是否显示
show: {
type: Boolean,
value: false,
observer: function (v) {
if (v && this.properties.value) {
let curr = toDate(this.properties.value);
this.setData({
selectDate: curr,
currYear: curr.getFullYear(),
currMonth: curr.getMonth()
})
if (this.properties.end) {
this.setData({
endDate: toDate(this.properties.end)
})
}
if (!this.data.path.length) {
this.initDate()
}
}
}
},
},
data: {
path: [],
currYear: new Date().getFullYear(),
currMonth: new Date().getMonth(),
currDay: new Date().getDate(),
selectDate: null,
endDate: null,
week: ["日", "一", "二", "三", "四", "五", "六"],
timeList: [],
startTime: [],
endTime: [],
startTimeValue: [],
endTimeValue: [],
},
ready() {
if (this.properties.value) {
this.setData({
selectDate: formatDate(this.properties.value, this.properties.format)
})
}
if (this.properties.end && this.properties.more) {
this.setData({
endDate: formatDate(this.properties.end, this.properties.format)
})
}
if (this.properties.time) {
if (typeof this.properties.time == 'boolean') {
this.setData({
timeList: [
Array.from(new Array(24).keys()).map(v => { return v < 10 ? ('0' + v) : v }),
Array.from(new Array(60).keys()).map(v => { return v < 10 ? ('0' + v) : v }),
Array.from(new Array(60).keys()).map(v => { return v < 10 ? ('0' + v) : v })
]
})
} else {
if (this.properties.time == 'hour') {
this.setData({
timeList: [
Array.from(new Array(24).keys()).map(v => { return v < 10 ? ('0' + v) : v })
]
})
} else if (this.properties.time == "minute") {
this.setData({
timeList: [
Array.from(new Array(24).keys()).map(v => { return v < 10 ? ('0' + v) : v }),
Array.from(new Array(60).keys()).map(v => { return v < 10 ? ('0' + v) : v }),
]
})
} else {
this.setData({
timeList: [
Array.from(new Array(24).keys()).map(v => { return v < 10 ? ('0' + v) : v }),
Array.from(new Array(60).keys()).map(v => { return v < 10 ? ('0' + v) : v }),
Array.from(new Array(60).keys()).map(v => { return v < 10 ? ('0' + v) : v })
]
})
}
}
}
this.setData({
startTime: this.data.timeList.map((m, i) => {
if (!i) {
return m[m.findIndex(f => Number(f) == new Date().getHours())]
} else if (i == 1) {
return m[m.findIndex(f => Number(f) == new Date().getMinutes())]
} else if (i == 2) {
return m[m.findIndex(f => Number(f) == new Date().getSeconds())]
}
}).join(':'),
startTimeValue: this.data.timeList.map((m, i) => {
if (!i) {
return m.findIndex(f => Number(f) == new Date().getHours())
} else if (i == 1) {
return m.findIndex(f => Number(f) == new Date().getMinutes())
} else if (i == 2) {
return m.findIndex(f => Number(f) == new Date().getSeconds())
}
})
})
if (this.properties.more) {
this.setData({
endTime: this.data.timeList.map((m, i) => {
if (!i) {
return m[m.findIndex(f => Number(f) == new Date().getHours())]
} else if (i == 1) {
return m[m.findIndex(f => Number(f) == new Date().getMinutes())]
} else if (i == 2) {
return m[m.findIndex(f => Number(f) == new Date().getSeconds())]
}
}).join(':'),
endTimeValue: this.data.timeList.map((m, i) => {
if (!i) {
return m.findIndex(f => Number(f) == new Date().getHours())
} else if (i == 1) {
return m.findIndex(f => Number(f) == new Date().getMinutes())
} else if (i == 2) {
return m.findIndex(f => Number(f) == new Date().getSeconds())
}
})
})
}
this.initDate()
},
methods: {
changeTimeStart(e) {
this.setData({
startTime: e.detail.value.map((m, i) => {
return this.data.timeList[i][m]
}).join(':')
});
},
changeTimeEnd(e) {
this.setData({
endTime: e.detail.value.map((m, i) => {
return this.data.timeList[i][m]
}).join(':')
});
},
initDate() {
// 本月1号的时间对象
let currDate = new Date(this.data.currYear, this.data.currMonth, 1);
// 本月1号星期几
let week = currDate.getDay();
// 日历上第一行第一列的开始时间
let startDay = currDate - week * 60 * 60 * 1000 * 24;
let list = [];
let startDate = this.data.selectDate && formatDate(this.data.selectDate) || "";
let endDate = this.data.endDate && formatDate(this.data.endDate) || "";
Array.from(new Array(42).keys()).forEach(v => {
let select = false;
let time = new Date(startDay + v * 60 * 60 * 1000 * 24);
let currtime = formatDate(time);
select = startDate == currtime;
if (this.properties.more && startDate && endDate) {
if (currtime > startDate && currtime < endDate) {
select = true;
}
}
let disable = `${time.getFullYear()}-${time.getMonth()}` != `${this.data.currYear}-${this.data.currMonth}`;
if (typeof this.properties.limit == "string" && !this.properties.limit.includes('[')) {
if (this.properties.limit.includes('&&') && !disable) {
let currStart = formatDate(this.properties.limit.split('&&')[0]);
let currEnd = formatDate(this.properties.limit.split('&&')[1]);
if (currtime > currEnd || currtime < currStart) {
disable = true;
}
}
if (this.properties.limit.includes('||') && !disable) {
let currStart = formatDate(this.properties.limit.split('||')[0]);
let currEnd = formatDate(this.properties.limit.split('||')[1]);
if (currtime < currEnd && currtime > currStart) {
disable = true;
}
}
if (this.properties.limit.includes('>') && !disable) {
let curr = formatDate(this.properties.limit.split('||')[0] || this.properties.limit.split('||')[1]);
if (currtime < curr) {
disable = true;
}
}
if (this.properties.limit.includes('<') && !disable) {
let curr = formatDate(this.properties.limit.split('||')[0] || this.properties.limit.split('||')[1]);
if (currtime > curr) {
disable = true;
}
}
} else {
if (this.properties.limit.length && !disable) {
if (typeof this.properties.limit == "string") {
disable = this.properties.limit.replace(/[\[\]]/g, '').split(',').map(m => formatDate(m)).some(m => m == currtime)
} else {
disable = this.properties.limit.map(m => formatDate(m)).some(m => m == currtime)
}
}
}
list.push({
time,
label: time.getDate(),
start: startDate,
end: endDate,
value: formatDate(time),
disable: disable,
select: select
})
})
this.setData({
path: list
})
// console.log(list)
},
onCell(e) {
let item = e.currentTarget.dataset.item;
if (item.disable) return;
if (this.properties.more) {
if (this.data.selectDate && this.data.endDate) {
this.setData({
selectDate: item.value,
endDate: "",
})
} else {
if (!this.data.selectDate) {
this.setData({
selectDate: item.value,
})
}
else if (this.data.selectDate > item.value) {
this.setData({
selectDate: item.value,
endDate: this.data.selectDate,
})
if (!this.properties.button) {
this.btnOk()
}
} else {
this.setData({ endDate: item.value })
if (!this.properties.button) {
this.btnOk()
}
}
}
} else {
this.setData({
selectDate: item.value
})
if (!this.properties.button) {
this.btnOk()
}
}
this.initDate()
},
changeYearMonth(e) {
this.setData({
currYear: e.detail.value.split('-')[0],
currMonth: Number(e.detail.value.split('-')[1]) - 1
});
this.initDate();
},
closeDate() {
this.triggerEvent('change', { value: "" })
},
toYear(e) {
if (e.currentTarget.dataset.type == '加') {
this.setData({
currYear: Number(this.data.currYear) + 1
})
} else {
this.setData({
currYear: Number(this.data.currYear) - 1
})
}
this.initDate()
},
btnOk() {
if (this.properties.more) {
if (this.properties.time) {
if (!this.data.startTimeValue && !this.data.endTimeValue && !this.data.selectDate && !this.data.endDate) {
this.properties.message && wx.showToast({ title: this.properties.message === true ? '时间未选择完' : this.properties.message, icon: "none", mask: true, duration: 6000 })
} else {
this.triggerEvent('change', { value: formatDate(this.data.selectDate + ' ' + this.data.startTime, this.properties.format), end: formatDate(this.data.endDate + ' ' + this.data.endTime, this.properties.format) })
}
} else {
if (!this.data.selectDate && !this.data.endDate) {
this.properties.message && wx.showToast({ title: this.properties.message === true ? '时间未选择完' : this.properties.message, icon: "none", mask: true, duration: 6000 })
} else {
this.triggerEvent('change', { value: formatDate(this.data.selectDate, this.properties.format), end: formatDate(this.data.endDate, this.properties.format) })
}
}
} else {
if (this.properties.time) {
if (!this.data.startTimeValue && !this.data.selectDate) {
wx.showToast({ title: '时间未选择完', icon: "none", mask: true, duration: 6000 })
} else {
this.triggerEvent('change', { value: formatDate(this.data.selectDate + ' ' + this.data.startTime, this.properties.format) })
}
} else {
if (!this.data.selectDate) {
wx.showToast({ title: '时间未选择完', icon: "none", mask: true, duration: 6000 })
} else {
this.triggerEvent('change', { value: formatDate(this.data.selectDate, this.properties.format) })
}
}
}
},
toMonth(e) {
if (e.currentTarget.dataset.type == '加') {
let month = Number(this.data.currMonth) + 1;
if (month >= 11) {
this.setData({
currMonth: 0,
currYear: Number(this.data.currYear) + 1
})
} else {
this.setData({
currMonth: month
})
}
} else {
let month = Number(this.data.currMonth) - 1;
if (month >= 0) {
this.setData({
currMonth: month
})
} else {
this.setData({
currMonth: 11,
currYear: Number(this.data.currYear) - 1
})
}
}
this.initDate()
}
}
});