UNPKG

@synotech/utils

Version:

a collection of utilities for internal use

41 lines (37 loc) 893 B
import { format as dfnFormat } from 'date-fns'; /** * This method validates email address * @module dateFormat * @param {date} date - a date object * @param @param {"full" | "long" | "short" | "time"} format - The format of the output. * @return {object} {Object} a well structured json object * @example * * dateFormat(new Date(), 'full') // returns 'Sun, Jan 3rd 2021 20:00:00' * */ export const dateFormat = ( date: any, format?: 'full' | 'long' | 'short' | 'time' ): any => { date = date?.iso ? date?.iso : date ? date : new Date(); const full = 'eee, MMM do, yyyy HH:mm'; let style; switch (format) { case 'full': style = full; break; case 'long': style = 'eee, MMM do, yyyy'; break; case 'short': style = 'MMM do, yyyy'; break; case 'time': style = 'HH:mm aaa'; break; default: style = full; } return dfnFormat(date, style); };