-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add parse and format functions for all date formats
BREAKING CHANGE: remove MINIMAL_VALID_DATE and ISO_GERMAN_DATE_FORMAT
- Loading branch information
Showing
1 changed file
with
35 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,72 @@ | ||
import { differenceInCalendarDays, format, isValid, parse } from 'date-fns'; | ||
import de from 'date-fns/locale/de'; | ||
|
||
export const GERMAN_DOTLESS_DATE_FORMAT: string = 'ddMMy'; | ||
export const GERMAN_DATE_FORMAT: string = 'dd.MM.y'; | ||
/** | ||
* Reverse order of standard german format to sort correctly. | ||
*/ | ||
export const ISO_GERMAN_DATE_FORMAT: string = 'y.MM.dd'; | ||
export const GERMAN_DATE_TIME_FORMAT: string = 'dd.MM.y HH:mm'; | ||
export const ISO_DATE_FORMAT: string = 'y-MM-dd'; | ||
export const ISO_DATE_TIME_FORMAT: string = 'y-MM-dd HH:mm:ss'; | ||
export const SECONDLESS_DATE_TIME_FORMAT: string = 'y-MM-dd HH:mm'; | ||
|
||
export const MINIMAL_VALID_DATE = new Date(1900, 0, 1); | ||
export function isToday(date: Date | number) { | ||
return differenceInCalendarDays(new Date(), date) === 0; | ||
} | ||
|
||
export function parseDate(value: string, formatString: string): Date | null { | ||
const parsedDate = parse(value, formatString, new Date()); | ||
|
||
return isValid(parsedDate) ? parsedDate : null; | ||
} | ||
|
||
export function parseGermanDateFlexible(value: string): Date | null { | ||
return parseGermanDotlessDate(value) || parseGermanDate(value); | ||
} | ||
|
||
export const GERMAN_DATE_FORMAT: string = 'dd.MM.y'; | ||
export function parseGermanDate(value: string): Date | null { | ||
return parseDate(value, GERMAN_DATE_FORMAT); | ||
} | ||
export function formatGerman(date: Date): string { | ||
return format(date, GERMAN_DATE_FORMAT, { locale: de }); | ||
} | ||
|
||
export const ISO_DATE_FORMAT: string = 'y-MM-dd'; | ||
export function parseIsoDate(value: string): Date | null { | ||
return parseDate(value, ISO_DATE_FORMAT); | ||
} | ||
|
||
export function parseSecondlessDateTime(value: string): Date | null { | ||
return parseDate(value, SECONDLESS_DATE_TIME_FORMAT); | ||
export function formatIsoDate(date: number | Date): string { | ||
return format(date, ISO_DATE_FORMAT, { locale: de }); | ||
} | ||
|
||
export const GERMAN_DOTLESS_DATE_FORMAT: string = 'ddMMy'; | ||
export function parseGermanDotlessDate(value: string): Date | null { | ||
return parseDate(value, GERMAN_DOTLESS_DATE_FORMAT); | ||
} | ||
|
||
export function parseGermanDateFlexible(value: string): Date | null { | ||
return parseGermanDotlessDate(value) || parseGermanDate(value); | ||
export function formatGermanDotlessDate(date: Date): string { | ||
return format(date, GERMAN_DOTLESS_DATE_FORMAT, { locale: de }); | ||
} | ||
|
||
export function formatGerman(date: Date): string { | ||
return format(date, GERMAN_DATE_FORMAT, { locale: de }); | ||
export const SECONDLESS_DATE_TIME_FORMAT: string = 'y-MM-dd HH:mm'; | ||
export function parseSecondlessDateTime(value: string): Date | null { | ||
return parseDate(value, SECONDLESS_DATE_TIME_FORMAT); | ||
} | ||
|
||
export function formatIsoGerman(date: Date): string { | ||
return format(date, ISO_GERMAN_DATE_FORMAT, { locale: de }); | ||
export function formatSecondlessDateTime(dateTime: number | Date): string { | ||
return format(dateTime, SECONDLESS_DATE_TIME_FORMAT, { locale: de }); | ||
} | ||
|
||
export const GERMAN_DATE_TIME_FORMAT: string = 'dd.MM.y HH:mm'; | ||
export function parseGermanDateTime(value: string): Date | null { | ||
return parseDate(value, GERMAN_DATE_TIME_FORMAT); | ||
} | ||
export function formatGermanDateTime(date: Date): string { | ||
return format(date, GERMAN_DATE_TIME_FORMAT, { locale: de }); | ||
} | ||
|
||
export function formatIsoDate(date: number | Date): string { | ||
return format(date, ISO_DATE_FORMAT, { locale: de }); | ||
} | ||
|
||
export function formatSecondlessDateTime(dateTime: number | Date): string { | ||
return format(dateTime, SECONDLESS_DATE_TIME_FORMAT, { locale: de }); | ||
export const ISO_DATE_TIME_FORMAT: string = 'y-MM-dd HH:mm:ss'; | ||
export function parseIsoDateTime(value: string): Date | null { | ||
return parseDate(value, ISO_DATE_TIME_FORMAT); | ||
} | ||
|
||
export function formatIsoDateTime(dateTime: number | Date): string { | ||
return format(dateTime, ISO_DATE_TIME_FORMAT, { locale: de }); | ||
} | ||
|
||
export function isToday(date: Date | number) { | ||
return differenceInCalendarDays(new Date(), date) === 0; | ||
export const DOTLESS_DATE_FORMAT: string = 'yMMdd'; | ||
export function parseDotlessDate(value: string): Date | null { | ||
return parseDate(value, DOTLESS_DATE_FORMAT); | ||
} | ||
export function formatDotlessDate(dateTime: number | Date): string { | ||
return format(dateTime, DOTLESS_DATE_FORMAT, { locale: de }); | ||
} |