date.js
2.6 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
import range from './range.js'
const START_YEAR = 1900
const END_YEAR = 2100
const UNIT_YEAR = '年'
const UNIT_MONTH = '月'
const UNIT_DAY = '日'
const UNIT_HOUR = '时'
const UNIT_MINUTES = '分'
const UNIT_SECONDS = '秒'
function isLeepYear(y) {
return (y % 4 === 0) && (y % 100 !== 0 || y % 400 === 0)
}
function autoPrefixer(num) {
return num < 10 ? '0' + num : num;
}
function getDays(y, m) {
y = Number(y)
m = Number(m)
let endDay = null
switch (m){
case 1:
case 2:
endDay = isLeepYear(y) ? 29 : 28
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
endDay = 31
break;
default:
endDay = 30
break;
}
const days = range(1, endDay, true, UNIT_DAY)
return days.map((day) => {
return {
value: day
// ,children: []
}
})
}
const yearData = range(START_YEAR, END_YEAR, false, UNIT_YEAR);
const monthData = range(1, 12, false, UNIT_MONTH);
const hourData = range(0, 23, true, UNIT_HOUR);
const minutesData = range(0, 59, true, UNIT_MINUTES);
const secondsData = range(0, 59, true, UNIT_SECONDS);
console.log(hourData)
console.log(minutesData)
console.log(secondsData)
const cascadeMonthData = monthData.map((month) => {
return {
value: month,
children: []
}
})
const dateData = yearData.map((year) => {
const item = {
value: year,
children: cascadeMonthData.slice()
}
item.children.forEach((month) => {
month.children = getDays(year.slice(0, -1), month.value.slice(0, -1))
})
return item;
})
// const customDateData = yearData.map((year) => {
// const item = {
// value: year,
// children: cascadeMonthData.slice()
// }
// item.children.forEach((month) => {
// month.children = getDays(year.slice(0, -1), month.value.slice(0, -1))
// month.children.forEach((day) => {
// day.children = hourData.map(hour => {
// return {
// value: hour,
// children: []
// }
// });
// day.children.forEach((hourItem) => {
// hourItem.children = minutesData.map(minute => {
// return {
// value: minute,
// children: []
// }
// })
// hourItem.children.forEach(minuteItem => {
// minuteItem.children = secondsData.map(second => {
// return { value: second }
// })
// })
// })
// })
// })
// return item;
// })
const date = new Date()
const dateAnchor = [
{ value: `${autoPrefixer(date.getFullYear())}${UNIT_YEAR}` },
{ value: `${autoPrefixer(date.getMonth() + 1)}${UNIT_MONTH}` },
{ value: `${autoPrefixer(date.getDate())}${UNIT_DAY}` },
]
// console.log('customDateData', customDateData)
export default {
getDateData() {
return dateData
}
}