2020-08-22 10:48:55 +03:00
|
|
|
import * as moment from 'moment';
|
2020-08-26 21:03:23 +03:00
|
|
|
import { OptionalString } from '../utils';
|
2020-08-22 10:48:55 +03:00
|
|
|
|
|
|
|
type MomentOrString = moment.Moment | string;
|
|
|
|
type NullableDate = MomentOrString | null;
|
|
|
|
|
2020-08-26 21:03:23 +03:00
|
|
|
const isMomentObject = (date: MomentOrString): date is moment.Moment => typeof (date as moment.Moment).format === 'function';
|
2020-08-22 10:48:55 +03:00
|
|
|
|
2020-08-26 21:03:23 +03:00
|
|
|
const formatDateFromFormat = (date?: NullableDate, format?: string): OptionalString =>
|
2020-08-22 10:48:55 +03:00
|
|
|
!date || !isMomentObject(date) ? date : date.format(format);
|
|
|
|
|
|
|
|
export const formatDate = (format = 'YYYY-MM-DD') => (date?: NullableDate) => formatDateFromFormat(date, format);
|
|
|
|
|
2020-08-22 19:32:48 +03:00
|
|
|
export const formatIsoDate = (date?: NullableDate) => formatDateFromFormat(date, undefined);
|
2020-12-15 00:58:15 +03:00
|
|
|
|
|
|
|
export const formatInternational = formatDate();
|