import moment, { unitOfTime } from 'moment-timezone'; import { DateType } from '../models'; import { format, parse } from 'date-fns'; export const MYSQL_HOURLY_FORMAT = '%Y-%m-%dT%H:00:00.000Z'; export const DATE_TIME_FORMAT_WITH_TIMEZONE = 'DD-MM-YYYY hh:mm:ss Z'; export const DATE_TIME_FORMAT = 'DD-MM-YYYY hh:mm:ss'; export const DATE_FORMAT = 'YYYY-MM-DD'; export const OPEN_REDMIT_DATE_FORMAT = 'YYYYMMDD'; export const FNS_DATE_FORMAT = 'dd-MM-yyyy'; export const FNS_DATE_TIME_FORMAT = 'dd/MM/yyyy HH:mm:ss'; export const FNS_DATE_ACTION_FORMAT = 'dd MMMM yyyy'; export const FNS_DATE_TIME_ACTION_FORMAT = 'dd MMMM yyyy, hh:mm a'; export const FNS_DATE_TIME_UTC_ACTION_FORMAT = 'dd-MM-yyyy hh:mm:ss a'; export const FNS_DATE_TIME_AUDIT_LOG_FORMAT = 'dd-MM-yyyy HH:mm:ss (z)'; export const FNS_DATE_HOUR = 'dd-MM-yyyy HH:mm'; export const formatDate = (date: DateType) => format(new Date(date), FNS_DATE_FORMAT); export const formatDateHour = (date: DateType) => format(new Date(date), FNS_DATE_HOUR); export const formatDateTime = (date: DateType) => format(new Date(date), FNS_DATE_TIME_FORMAT); export const formatDateTimeAction = (date: DateType) => format(new Date(date), FNS_DATE_TIME_ACTION_FORMAT); export const formatDateTimeUTCAction = (date: DateType) => format(new Date(date), FNS_DATE_TIME_UTC_ACTION_FORMAT); export const formatDateTimeAuditLog = (date: DateType) => format(new Date(date), FNS_DATE_TIME_AUDIT_LOG_FORMAT); export const formatDateAction = (date: DateType) => format(new Date(date), FNS_DATE_ACTION_FORMAT); export const formatDateWithFormat = (date: DateType, _format: string) => format(new Date(date), _format); export function formatDateTimeTZ( date: DateType, timezone: string = 'UTC' ): string { return moment(date).tz(timezone).format(DATE_TIME_FORMAT_WITH_TIMEZONE); } export function formatDateTZ(date: DateType, timezone: string = 'UTC'): string { if (!date) { return ''; } return moment(date).tz(timezone).format(DATE_FORMAT); } export function getIsoWeek(): { week: number; year: number } { const _moment = moment(); return { week: _moment.isoWeek(), year: _moment.isoWeekYear(), }; } export function nextDays(days: number): Date { const _nextDate = moment().add(days, 'days'); return _nextDate.toDate(); } export function addMonths(date: DateType, months: number): Date { return moment(date).add(months, 'months').toDate(); } export function addHours(date: DateType, hours: number): Date { return moment(date).add(hours, 'hours').toDate(); } export function addMinutes(date: DateType, minutes: number): Date { return moment(date).add(minutes, 'minutes').toDate(); } export function differentMonth(date: DateType, date1: DateType): number { return Math.ceil(moment(date).diff(moment(date1), 'months', true)); } export function differentSecond(date: DateType, date1: DateType): number { return Math.ceil(moment(date).diff(moment(date1), 'seconds', true)); } export function differentMin(date: DateType, date1: DateType): number { return Math.ceil(moment(date).diff(moment(date1), 'minutes', true)); } export function getHourRange(date?: Date): { begin: Date; end: Date } { const hourBegin = date ? new Date(date.getTime()) : new Date(); const hourTo = date ? new Date(date.getTime()) : new Date(); hourBegin.setMinutes(0); hourBegin.setSeconds(0); hourBegin.setMilliseconds(0); hourTo.setMinutes(59); hourTo.setSeconds(59); hourTo.setMilliseconds(999); return { begin: hourBegin, end: hourTo, }; } export function formatOpenRedmitDate(date: DateType): string { return moment(date).format(OPEN_REDMIT_DATE_FORMAT); } // format date from format to another format export const formatDateFromFormat = ( date: string, fromFormat: string, toFormat: string ) => { return format(parse(date, fromFormat, new Date()), toFormat); }; // function to get start date of today export const getStartDate = () => { const date = new Date(); date.setHours(0, 0, 0, 0); return date; }; // function to get end date of today export const getEndDate = () => { const date = new Date(); date.setHours(23, 59, 59, 999); return date; }; // function to get start date of current month export const getStartDateOfMonth = () => { const date = new Date(); date.setDate(1); date.setHours(0, 0, 0, 0); return date; }; export const MAX_TO_DATE = getEndDate(); /** * @param d1 {Date | string} greater date * @param d2 {Date | string} smaller date * @param f {string} format * @param u unit * @returns {number} */ export const diffDateWithFormat = ( d1: DateType, d2: DateType, f: string, u: unitOfTime.Diff ) => moment(d1, f, true).diff(moment(d2, f, true), u); export function formatTimeTZ(date: DateType, timezone: string, format: string) { return moment(date).tz(timezone).format(format); } export function formatTimeUTC(date: DateType, format = 'DD-MM-yyyy HH:mm') { return formatTimeTZ(date, 'UTC', format); } export function openRemitDate(d: DateType) { if (!d) { return d; } return formatDateWithFormat(d, 'yyyyMMdd'); } export function formatMilliseconds(ms: number) { if (!ms || ms <= 0) { return '0s'; } const seconds = Math.floor(ms / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); const months = Math.floor(days / 30); const years = Math.floor(months / 12); if (years > 0) { return years + 'y'; } else if (months > 0) { return months + 'm'; } else if (days > 0) { return days + 'd'; } else if (hours > 0) { return hours + 'h'; } else if (minutes > 0) { return minutes + 'm'; } else if (seconds > 0) { return seconds + 's'; } else { return ms + 'ms'; } }