99 lines
2.2 KiB
JavaScript
99 lines
2.2 KiB
JavaScript
function pad(value) {
|
|||
|
|
return String(value).padStart(2, '0')
|
||
|
|
}
|
||
|
|
|
||
|
|
function toDate(value) {
|
||
|
|
if (value instanceof Date) {
|
||
|
|
return new Date(value.getTime())
|
||
|
|
}
|
||
|
|
return value ? new Date(String(value).replace(/-/g, '/')) : new Date()
|
||
|
|
}
|
||
|
|
|
||
|
|
function formatDate(value) {
|
||
|
|
const target = toDate(value)
|
||
|
|
return `${target.getFullYear()}-${pad(target.getMonth() + 1)}-${pad(target.getDate())}`
|
||
|
|
}
|
||
|
|
|
||
|
|
function formatMonth(value) {
|
||
|
|
const target = toDate(value)
|
||
|
|
return `${target.getFullYear()}年${target.getMonth() + 1}月`
|
||
|
|
}
|
||
|
|
|
||
|
|
function formatCnDate(value) {
|
||
|
|
const target = toDate(value)
|
||
|
|
return `${target.getMonth() + 1}月${target.getDate()}日`
|
||
|
|
}
|
||
|
|
|
||
|
|
function addDays(value, days) {
|
||
|
|
const target = toDate(value)
|
||
|
|
target.setDate(target.getDate() + days)
|
||
|
|
return target
|
||
|
|
}
|
||
|
|
|
||
|
|
function addMonths(value, months) {
|
||
|
|
const target = toDate(value)
|
||
|
|
target.setMonth(target.getMonth() + months)
|
||
|
|
return target
|
||
|
|
}
|
||
|
|
|
||
|
|
function startOfWeek(value) {
|
||
|
|
const target = toDate(value)
|
||
|
|
const day = target.getDay() || 7
|
||
|
|
target.setDate(target.getDate() - day + 1)
|
||
|
|
return target
|
||
|
|
}
|
||
|
|
|
||
|
|
function endOfWeek(value) {
|
||
|
|
return addDays(startOfWeek(value), 6)
|
||
|
|
}
|
||
|
|
|
||
|
|
function startOfMonth(value) {
|
||
|
|
const target = toDate(value)
|
||
|
|
return new Date(target.getFullYear(), target.getMonth(), 1)
|
||
|
|
}
|
||
|
|
|
||
|
|
function endOfMonth(value) {
|
||
|
|
const target = toDate(value)
|
||
|
|
return new Date(target.getFullYear(), target.getMonth() + 1, 0)
|
||
|
|
}
|
||
|
|
|
||
|
|
function monthGrid(value) {
|
||
|
|
const first = startOfMonth(value)
|
||
|
|
const startOffset = (first.getDay() || 7) - 1
|
||
|
|
const gridStart = addDays(first, -startOffset)
|
||
|
|
const days = []
|
||
|
|
for (let i = 0; i < 42; i += 1) {
|
||
|
|
const current = addDays(gridStart, i)
|
||
|
|
days.push({
|
||
|
|
date: formatDate(current),
|
||
|
|
day: current.getDate(),
|
||
|
|
isCurrentMonth: current.getMonth() === first.getMonth()
|
||
|
|
})
|
||
|
|
}
|
||
|
|
return days
|
||
|
|
}
|
||
|
|
|
||
|
|
function minutesOfDay(value) {
|
||
|
|
const parts = String(value || '00:00').split(':').map(Number)
|
||
|
|
return (parts[0] || 0) * 60 + (parts[1] || 0)
|
||
|
|
}
|
||
|
|
|
||
|
|
function timeFromMinutes(minutes) {
|
||
|
|
return `${pad(Math.floor(minutes / 60))}:${pad(minutes % 60)}`
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
formatDate,
|
||
|
|
formatMonth,
|
||
|
|
formatCnDate,
|
||
|
|
addDays,
|
||
|
|
addMonths,
|
||
|
|
startOfWeek,
|
||
|
|
endOfWeek,
|
||
|
|
startOfMonth,
|
||
|
|
endOfMonth,
|
||
|
|
monthGrid,
|
||
|
|
minutesOfDay,
|
||
|
|
timeFromMinutes
|
||
|
|
}
|