date.js 2.6 KB
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
	}
}