replace momentjs (incl. calendar)

This commit is contained in:
Thomas Ruoff
2020-09-22 00:21:08 +02:00
committed by Thomas Ruoff
parent 7f4604f0e6
commit 02c2b45747
6 changed files with 161 additions and 115 deletions

View File

@@ -1,50 +1,49 @@
import moment from 'moment'
import { parse, format, addDays } from 'date-fns'
const FRONTEND_FORMAT = 'DD.MM.YYYY'
const BACKEND_FORMAT = 'YYYY-MM-DD'
const FRONTEND_FORMAT = 'dd.MM.yyyy'
const BACKEND_FORMAT = 'yyyy-MM-dd'
export function getDays({
startDate,
endDate,
}: {
startDate: moment.MomentInput
endDate: moment.MomentInput
startDate: Date
endDate: Date
}) {
let currentDay = moment(startDate)
let currentDay = new Date(startDate.getTime())
const days = [dateFormatBackend(currentDay)]
if (!endDate) {
return days
}
const end = moment(endDate)
while (currentDay < end) {
currentDay = currentDay.add(1, 'day')
while (currentDay < endDate) {
currentDay = addDays(currentDay, 1)
days.push(dateFormatBackend(currentDay))
}
return days
}
function dateFormat(date: moment.MomentInput, format: string) {
function dateFormat(date: Date, formatString: string) {
if (!date) {
return null
}
return moment(date).format(format)
return format(date, formatString)
}
export function dateFormatBackend(date: moment.MomentInput) {
export function dateFormatBackend(date: Date) {
return dateFormat(date, BACKEND_FORMAT)
}
export function dateFormatFrontend(date: moment.MomentInput) {
export function dateFormatFrontend(date: Date) {
return dateFormat(date, FRONTEND_FORMAT)
}
function dateParse(input: string, format: string) {
const date = moment(input, format)
if (date.isValid()) {
return date.toDate()
function dateParse(input: string, formatString: string) {
const date = parse(input, formatString, new Date())
if (date.getTime() !== NaN) {
return date
}
return null