/** * The isValidDate function in TypeScript validates if a given date string is in the format YYYY-MM-DD. * @param {string} date - The `isValidDate` function you provided checks if a given date string is in * the format `YYYY-MM-DD`. If the date string does not match this format, it throws an error * indicating the correct format. */ export const isValidDate = (date: string) => { const regex = /^\d{4}-\d{2}-\d{2}$/; if (!regex.test(date)) { throw new Error( `Invalid format: ${date}, the correct format is YYYY-MM-DD` ); } }; /** * Validates that a string is a full ISO-8601 date-time with a timezone. * Examples of valid formats: * - 2026-02-23T12:00:00Z * - 2026-02-23T12:00:00+01:00 * - 2026-02-23T12:00:00.000-06:00 */ export const isValidZonedDateTime = (date: string) => { // Regex for ISO 8601 with Timezone const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/; if (!isoRegex.test(date)) { throw new Error( `Invalid format: ${date}. Expected ISO-8601 with Timezone (e.g., YYYY-MM-DDTHH:mm:ssZ or YYYY-MM-DDTHH:mm:ss+HH:mm)` ); } // Also verify it's a valid calendar date if (isNaN(Date.parse(date))) { throw new Error(`Invalid date value: ${date}`); } };