mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-04-02 11:03:35 +03:00
23 lines
838 B
TypeScript
23 lines
838 B
TypeScript
import { format, formatISO, parse } from 'date-fns';
|
|
import { OptionalString } from '../utils';
|
|
|
|
type DateOrString = Date | string;
|
|
type NullableDate = DateOrString | null;
|
|
|
|
export const isDateObject = (date: DateOrString): date is Date => typeof date !== 'string';
|
|
|
|
const formatDateFromFormat = (date?: NullableDate, theFormat?: string): OptionalString => {
|
|
if (!date || !isDateObject(date)) {
|
|
return date;
|
|
}
|
|
|
|
return theFormat ? format(date, theFormat) : formatISO(date);
|
|
};
|
|
|
|
export const formatDate = (format = 'yyyy-MM-dd') => (date?: NullableDate) => formatDateFromFormat(date, format);
|
|
|
|
export const formatIsoDate = (date?: NullableDate) => formatDateFromFormat(date, undefined);
|
|
|
|
export const formatInternational = formatDate();
|
|
|
|
export const parseDate = (date: string, format: string) => parse(date, format, new Date());
|