mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
50 lines
969 B
JavaScript
50 lines
969 B
JavaScript
import moment from 'moment'
|
|
|
|
const FRONTEND_FORMAT = 'DD.MM.YYYY'
|
|
const BACKEND_FORMAT = 'YYYY-MM-DD'
|
|
|
|
export function getDays({ startDate, endDate }) {
|
|
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, format) {
|
|
if (!date) {
|
|
return null
|
|
}
|
|
return moment(date).format(format)
|
|
}
|
|
|
|
export function dateFormatBackend(date) {
|
|
return dateFormat(date, BACKEND_FORMAT)
|
|
}
|
|
|
|
export function dateFormatFrontend(date) {
|
|
return dateFormat(date, FRONTEND_FORMAT)
|
|
}
|
|
|
|
function dateParse(string, format) {
|
|
const date = moment(string, format)
|
|
if (date.isValid()) {
|
|
return date.getDate()
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
export function dateParseFrontend(string) {
|
|
return dateParse(string, FRONTEND_FORMAT)
|
|
}
|