date.js
1.37 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
import range from './range'
const START_YEAR = 1900
const END_YEAR = 2100
const UNIT_YEAR = '年'
const UNIT_MONTH = '月'
const UNIT_DAY = '日'
function isLeapYear (y) {
return (y % 4 === 0) && (y % 100 !== 0 || y % 400 === 0)
};
function getDays (y, m) {
y = Number(y)
m = Number(m)
let endDay = null
switch (m) {
case 2:
endDay = isLeapYear(y) ? 29 : 28; break
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
endDay = 31; break
case 4:
case 6:
case 9:
case 11:
default:
endDay = 30; break
}
const days = range(1, endDay, false, UNIT_DAY)
return days.map((day) => {
return { value: day }
})
};
const yearData = range(START_YEAR, END_YEAR, false, UNIT_YEAR)
const monthData = range(1, 12, false, UNIT_MONTH)
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 date = new Date()
const dateAnchor = [
{ value: `${date.getFullYear()}${UNIT_YEAR}` },
{ value: `${date.getMonth() + 1}${UNIT_MONTH}` },
{ value: `${date.getDate()}${UNIT_DAY}` }
]
export {
dateAnchor,
dateData
}