shlink-web-client/src/utils/helpers/date.ts

17 lines
704 B
TypeScript
Raw Normal View History

import * as moment from 'moment';
2020-08-26 21:03:23 +03:00
import { OptionalString } from '../utils';
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-26 21:03:23 +03:00
const formatDateFromFormat = (date?: NullableDate, format?: string): OptionalString =>
!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);
export const formatInternational = formatDate();