Skip to content

Instantly share code, notes, and snippets.

View dabydat's full-sized avatar
:shipit:
I may be slow to respond.

David A. Gómez Rueda dabydat

:shipit:
I may be slow to respond.
View GitHub Profile
@dabydat
dabydat / custom-dates.ts
Created January 22, 2025 17:16
You will find two function in this gist. The first one you will validate if the date that is passing by is correct. The second one is for format the date, you must send the format of the date and the format you finally want and obviously the value of the date you want to format
type Format = 'YYYYMMDD' | 'MMDDYYYY' | 'DDMMYYYY';
const isValidDate = (value: string, format: Format): boolean => {
const DATEFORMAT =
format == 'YYYYMMDD'
? /^([12]\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
: format == 'MMDDYYYY'
? /^(0?[1-9]|1[0-2])\/(0?[1-9]|[1-2][0-9]|3[01])\/\d{4}$/
: /^(0?[1-9]|[1-2][0-9]|3[01])\/(0?[1-9]|1[0-2])\/\d{4}$/;
const isValid = value.match(DATEFORMAT);