-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework date comparison and formatting
- Loading branch information
1 parent
45fab45
commit f8dcc20
Showing
2 changed files
with
29 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
import {DateTime} from 'luxon' | ||
|
||
export const formatDateTime = (date: string) => DateTime.fromISO(date).toFormat('dd. MM. yyyy HH:mm') | ||
export const formatDate = (date: string) => DateTime.fromISO(date).toFormat('dd. LLL yyyy') | ||
export const formatDateDigits = (date: string) => DateTime.fromISO(date).toFormat('dd. MM. yyyy') | ||
export enum DateFormat { | ||
TIME = 'HH:mm', | ||
DATE = 'dd. LLL yyyy', | ||
DATE_TIME = 'dd. MM. yyyy HH:mm', | ||
DATE_DIGITS = 'dd. MM. yyyy', | ||
} | ||
|
||
export const formatDateTime = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE_TIME) | ||
export const formatDate = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE) | ||
export const formatDateDigits = (date: string) => DateTime.fromISO(date).toFormat(DateFormat.DATE_DIGITS) | ||
|
||
export function formatDateTimeInterval(date1: string, date2: string) { | ||
if (date1 === date2) return formatDateDigits(date1) | ||
const dateTime1 = DateTime.fromISO(date1) | ||
const dateTime2 = DateTime.fromISO(date2) | ||
if (dateTime1 === dateTime2) return formatDate(date1) | ||
if (dateTime1.toFormat('dd.MM.yyyy') === dateTime2.toFormat('dd.MM.yyyy')) | ||
return `${dateTime1.toFormat('dd.MM.yyyy')} ${dateTime1.toFormat('HH:mm')} - ${dateTime2.toFormat('HH:mm')}` | ||
return `${formatDateTime(date1)} - ${formatDateTime(date2)}` | ||
|
||
if (dateTime1.equals(dateTime2)) return formatDateDigits(date1) | ||
|
||
if (dateTime1.startOf('day').equals(dateTime2.startOf('day'))) | ||
return `${dateTime1.toFormat(DateFormat.DATE_DIGITS)} ${dateTime1.toFormat(DateFormat.TIME)} - ${dateTime2.toFormat( | ||
DateFormat.TIME, | ||
)}` | ||
|
||
return `${dateTime1.toFormat(DateFormat.DATE_TIME)} - ${dateTime2.toFormat(DateFormat.DATE_TIME)}` | ||
} |