date-picker.js
4.7 KB
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
Component({
properties: {
value: {
type: Array,
value: [],
observer: "onValue"
},
isShow: {
type: Boolean,
value: false,
observer: "onShow"
}
},
data: {
years: [],
months: [],
days: [],
tempYearPos: [0],
tempMonthPos: [0],
tempDayPos: [0],
showPicker: true
},
attached: function() {
/**
* 初始化年月日
*/
var date = new Date();
var years = [];
var months = [];
var days = [];
for (let i = 1900; i <= date.getFullYear(); i++) {
years.push(i);
}
for (let i = 1; i <= 12; i++) {
let month = 0;
month = i < 10 ? '0' + i : i;
months.push(month);
}
days = this.getDays(date.getFullYear(), date.getMonth() + 1);
this.setData({
years: years,
months: months,
days: days
});
},
methods: {
onTouchmask: function(event) {
},
onCacnelClick(e) {
this.triggerEvent('cancelclick', {});
},
onSureClick(e) {
var curYear = this.data.years[this.data.tempYearPos];
var curMonth = this.data.months[this.data.tempMonthPos];
var curDay = this.data.days[this.data.tempDayPos];
var value = [curYear, curMonth, curDay];
this.triggerEvent('sureclick', {
value: value,
});
},
year_onChange: function(e) {
//年改变,月要滑到一月,天要重新计算该年该月多少天
var days = [];
var curYear = this.data.years[e.detail.value];
days = this.getDays(curYear, 1);
this.setData({
days: days,
tempYearPos: e.detail.value,
tempMonthPos: [0],
tempDayPos: [0],
});
},
month_onChange: function(e) {
var days = [];
var curYear = this.data.years[this.data.tempYearPos];
var curMonth = this.data.months[e.detail.value];
days = this.getDays(curYear, curMonth);
this.setData({
days: days,
tempMonthPos: e.detail.value,
tempDayPos: [0],
});
},
day_onChange: function(e) {
this.setData({
tempDayPos: e.detail.value
});
},
onValue() {
//通过传进来的年月日,计算对应的index
var data = this.getRefreshData();
this.setData(data)
},
onShow() {
var data = this.getRefreshData();
data.showPicker = this.data.isShow;
this.setData(data)
},
getDays(year, month) {
var days = [];
month = parseInt(month, 10);
var date = new Date(year, month, 0);
var maxDay = date.getDate();
for (let i = 1; i <= maxDay; i++) {
let day = 0;
day = i < 10 ? '0' + i : i;
days.push(day);
}
return days;
},
getRefreshData() {
//通过传进来的年月日,计算对应的inde
if (this.data.years == null || this.data.years.length == 0) {
return {};
}
var date = new Date();
var tempYearPos = this.data.years.length - 1;
var tempMonthPos = date.getMonth();
var tempDayPos = date.getDate() - 1;
for (var i = 0; i < this.data.years.length; i++) {
var item = this.data.years[i];
if (item == this.data.value[0]) {
tempYearPos = i;
break;
}
}
for (var i = 0; i < this.data.months.length; i++) {
var item = this.data.months[i];
if (item == this.data.value[1]) {
tempMonthPos = i;
break;
}
}
var days = [];
var curYear = this.data.years[tempYearPos];
days = this.getDays(curYear, this.data.months[tempMonthPos]);
for (var i = 0; i < days.length; i++) {
var item = days[i];
if (item == this.data.value[2]) {
tempDayPos = i;
break;
}
}
var data = {
days: days,
tempYearPos: [tempYearPos],
tempMonthPos: [tempMonthPos],
tempDayPos: [tempDayPos],
}
return data;
},
}
});