dishplanner/frontend-old/app/helpers/Date.ts

18 lines
790 B
TypeScript

import { DateTime } from 'luxon';
// Validate if a given string matches the "yyyy-MM-dd" format and is a valid date
export const isValidDate = (date: string): boolean => {
const parsedDate = DateTime.fromFormat(date, 'yyyy-MM-dd');
return parsedDate.isValid && parsedDate.toFormat('yyyy-MM-dd') === date;
};
// Format a date to a specific string format
export const formatDate = (date: Date | string, format: string = 'yyyy-MM-dd'): string => {
const parsedDate = typeof date === 'string' ? DateTime.fromISO(date) : DateTime.fromJSDate(date);
return parsedDate.toFormat(format);
};
// Compare two dates to see if one is before the other
export const isBefore = (date1: string, date2: string): boolean => {
return DateTime.fromISO(date1) < DateTime.fromISO(date2);
};