mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
56 lines
1.1 KiB
TypeScript
56 lines
1.1 KiB
TypeScript
import moment from 'moment'
|
|
|
|
const FRONTEND_FORMAT = 'DD.MM.YYYY'
|
|
const BACKEND_FORMAT = 'YYYY-MM-DD'
|
|
|
|
export function getDays({
|
|
startDate,
|
|
endDate,
|
|
}: {
|
|
startDate: moment.MomentInput
|
|
endDate: moment.MomentInput
|
|
}) {
|
|
let currentDay = moment(startDate)
|
|
const days = [dateFormatBackend(currentDay)]
|
|
|
|
if (!endDate) {
|
|
return days
|
|
}
|
|
|
|
const end = moment(endDate)
|
|
while (currentDay < end) {
|
|
currentDay = currentDay.add(1, 'day')
|
|
days.push(dateFormatBackend(currentDay))
|
|
}
|
|
|
|
return days
|
|
}
|
|
|
|
function dateFormat(date: moment.MomentInput, format: string) {
|
|
if (!date) {
|
|
return null
|
|
}
|
|
return moment(date).format(format)
|
|
}
|
|
|
|
export function dateFormatBackend(date: moment.MomentInput) {
|
|
return dateFormat(date, BACKEND_FORMAT)
|
|
}
|
|
|
|
export function dateFormatFrontend(date: moment.MomentInput) {
|
|
return dateFormat(date, FRONTEND_FORMAT)
|
|
}
|
|
|
|
function dateParse(input: string, format: string) {
|
|
const date = moment(input, format)
|
|
if (date.isValid()) {
|
|
return date.toDate()
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
export function dateParseFrontend(input: string) {
|
|
return dateParse(input, FRONTEND_FORMAT)
|
|
}
|