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
var currentDate = new Date();
const weekList = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
const durationList = [{ value: 60, label: '1小时' }, { value: 120, label: '2小时' }, { value: 30, label: '30分钟' }];
const timeData = generateDate(365);
const timeHour = generateTime(24);
const timeMin = generateTime(60);
function padZero(val) {
return ("00" + val).slice(-2);
}
// 生成小时和分钟
function generateTime(max) {
const time = [];
for (let i = 0; i < max; i++) {
time.push(padZero(i))
}
return time;
}
// 生成年月日周
function generateDate(max) {
const timeData = [];
for (let i = max; i > 0; i--) {
timeData.push(GetDateStr(-i));
}
for (let i = 0; i < max; i++) {
timeData.push(GetDateStr(i));
}
return timeData
}
function GetDateStr(AddDayCount) {
var time = new Date();
time.setDate(time.getDate() + AddDayCount);
const res = getYMDWHMIN(time);
return res.y + '年' + res.m + "月" + res.d + "日 " + res.w;
}
// 获取年月日周时分
function getYMDWHMIN(date) {
const y = date.getFullYear();
const m = padZero(date.getMonth() + 1);
const d = padZero(date.getDate());
const w = weekList[date.getDay()];
const h = padZero(date.getHours());
const min = padZero(date.getMinutes());
return {
y, m, d, w, h, min
}
}
// 时间格式转化为03月01日 14:00
function getTabShowDateByDate(date) {
const res = getYMDWHMIN(date);
return res.m + "月" + res.d + "日 " + res.h + ':' + res.min;
}
function getdhmByPickValue(pickValue) {
const d = timeData[pickValue[0]];
const h = timeHour[pickValue[1]];
const m = timeMin[pickValue[2]];
return { d, h, m }
}
function getTabShowDateByPickValue(pickValue) {
const { d, h, m } = getdhmByPickValue(pickValue);
const time = `${d.slice(5, 10)} ${h}:${m}`;
const weekDay = d.slice(-2);
return { time, weekDay }
}
// 根据column值获取标准时间格式 2019/2/2 16:00
function getPostTimeByPickValue(pickValue) {
const { d, h, m } = getdhmByPickValue(pickValue);
const newD = d.slice(0, 10).replace('年', '/').replace('月', '/').replace('日', '/');
return newD + ' ' + h + ':' + m
}
// 将周几转换为今天,明天, 昨天
function getTabShowWeekByDate(futureData) {
const date = new Date(futureData);
let week = '今天';
//同年同月
if (currentDate.getFullYear() == date.getFullYear() &&
currentDate.getMonth() == date.getMonth()
) {
if (currentDate.getDate() == date.getDate()) {
week = '今天'
} else if (date.getDate() - currentDate.getDate() == 1) {
week = '明天'
} else if (currentDate.getDate() - date.getDate() == 1) {
week = '昨天'
} else {
week = weekList[date.getDay()]
}
} else {
week = weekList[date.getDay()]
}
return week;
}
// 根据开始时间和持续时间计算结束时间 2019/2/2 16:00
function getEndTime(startTime, duration) {
let newDate = new Date(startTime);
var min = newDate.getMinutes();
newDate.setMinutes(min + duration);
const res = getYMDWHMIN(newDate);
return res.y + '/' + res.m + '/' + res.d + ' ' + res.h + ':' + res.min;
}
// 获取结束至时间的显示 16:00
function getDurationEndTime(startTime, duration) {
return getEndTime(startTime, duration).slice(-5);
}
// 获取持续时间 {value,lable,index}
function getDuration(startDate, endDate) {
const time = endDate.getTime() - startDate.getTime();
const min = time / 1000 / 60;
let duration = null;
durationList.forEach((it, index) => {
if (it.value == min) {
it.index = index;
duration = it
}
})
return duration
}
// 根据持续时间的显示值获取实际分钟数
function getDurationValue(durationLable) {
let value = 120;
durationList.forEach(it => {
if (it.label == durationLable) {
value = it.value
}
});
return value
}
// 获取时间格式的pick value用于pick column回显
function getPickerValue(date) {
const res = getYMDWHMIN(date);
const pickerValue = [timeData.indexOf(res.y + "年" + res.m + "月" + res.d + "日 " + res.w), timeHour.indexOf(res.h), timeMin.indexOf(res.min)];
return pickerValue
}
Component({
mixins: [],
data: {
currentTab: 'start',
timeData: timeData,
timeHour: timeHour,
timeMin: timeMin,
durationList: durationList,
isEndTimeCustom: false,
//用于pick column回显
startPickValue: [],
endPickValue: [],
endDurationPickValue: [1],
// 当前 pick column回显
pickValue: [],
//开始、结束时间tab数据
startWeekDay: '今天',
startTime: '',
endWeekDay: '今天',
endTime: '',
//持续结束时间tab数据
duration: '2小时',
//已选择的标准时间格式数据
postStartTime: '',
postEndTime: '',
durationEndTime: '',
error: ''
},
props: {
},
didMount() {
this.setInitialValue();
},
didUpdate() {
},
didUnmount() { },
methods: {
//设置初始值
setInitialValue() {
let startDate = new Date();
//结束日期默认+2小时
let endDate = new Date();
var hour = endDate.getHours();
endDate.setHours(hour + 2);
// 赋值
if (this.props.startTime) {
startDate = new Data(this.props.startTime);
}
if (this.props.endTime) {
endDate = new Data(thia.props.endTime);
}
//开始和结束时间,持续时间赋值
this.setData({
startPickValue: getPickerValue(startDate),
startTime: getTabShowDateByDate(startDate),
postStartTime: startDate,
pickValue: getPickerValue(startDate),
endPickValue: getPickerValue(endDate),
endTime: getTabShowDateByDate(endDate),
postEndTime: endDate
});
// 如果持续时间不在范围内,需要改变默认显示结束时间的方式
const duration = getDuration(startDate, endDate);
if (duration) {
this.setData({
duration: duration.label,
durationEndTime: getDurationEndTime(startDate, duration.value),
endDurationPickValue: [duration.index]
})
} else {
this.setData({
isEndTimeCustom: true
})
}
},
onChange(e) {
const pickValue = e.detail.value;
const timeData = getTabShowDateByPickValue(pickValue);
//改变开始时间,改变持续时间的结束时间
if (this.data.currentTab === 'start') {
const postStartTime = getPostTimeByPickValue(pickValue);
this.setData({
startTime: timeData.time,
startWeekDay: getTabShowWeekByDate(postStartTime),
startPickValue: pickValue,
postStartTime
});
if (!this.data.isEndTimeCustom) {
this.setData({
durationEndTime: getDurationEndTime(postStartTime, getDurationValue(this.data.duration))
})
}
}
// 改变结束时间,这里结束时间和持续时间互不影响
else if (this.data.currentTab === 'end') {
const postEndTime = getPostTimeByPickValue(pickValue);
if (this.data.isEndTimeCustom) {
this.setData({
endTime: timeData.time,
endWeekDay: getTabShowWeekByDate(postEndTime),
endPickValue: pickValue,
postEndTime
});
} else {
const durationData = durationList[pickValue[0]];
this.setData({
duration: durationData.label,
durationEndTime: getDurationEndTime(this.data.postStartTime, durationData.value),
endDurationPickValue: pickValue,
postEndTime: getEndTime(this.data.postStartTime, durationData.value)
});
}
}
this.checkValue(this.data.postStartTime, this.data.postEndTime);
},
changeTab(e) {
const currentTab = e.currentTarget.id;
//pick coulum 回显
if (currentTab === 'end' && !this.data.isEndTimeCustom) {
return this.setData({
currentTab,
pickValue: this.data.endDurationPickValue
});
}
this.setData({
currentTab,
pickValue: this.data[`${currentTab}PickValue`]
});
},
changeWay() {
this.setData({
isEndTimeCustom: true
});
//如果在end tab 切换, 需要设置结束时间的pick column回显
if (this.data.currentTab == 'end') {
this.setData({
pickValue: this.data.endPickValue
});
}
},
checkValue(postStartTime, postEndTime) {
if (new Date(postEndTime).getTime() <= new Date(postStartTime).getTime()) {
this.setData({
error: '开始时间小于结束时间'
})
return false;
} else {
this.setData({
error: ''
})
return true
}
},
complete() {
const { postStartTime, postEndTime } = this.data;
if (this.checkValue(postStartTime, postEndTime)) {
this.props.onComplete({
startTime: postStartTime,
endTime: postEndTime
})
}
}
},
});