Files
pfadi-bussle/helpers/date.ts
Thomas Ruoff 90ac05a907 fix most type errors
still have a few things outstanding
2020-09-09 00:25:09 +02:00

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)
}