Created
January 22, 2025 17:16
-
-
Save dabydat/a0834b051b43bb3374398e61d08b9f22 to your computer and use it in GitHub Desktop.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| if (isValid === null) return false; | |
| let day!: number; | |
| let month!: number; | |
| let year!: number; | |
| const dateArray: string[] = value.split(format === 'YYYYMMDD' ? '-' : '/'); | |
| switch (format) { | |
| case 'YYYYMMDD': | |
| day = parseInt(dateArray[2]); | |
| month = parseInt(dateArray[1]); | |
| year = parseInt(dateArray[0]); | |
| break; | |
| case 'MMDDYYYY': | |
| day = parseInt(dateArray[1]); | |
| month = parseInt(dateArray[0]); | |
| year = parseInt(dateArray[2]); | |
| break; | |
| case 'DDMMYYYY': | |
| day = parseInt(dateArray[0]); | |
| month = parseInt(dateArray[1]); | |
| year = parseInt(dateArray[2]); | |
| break; | |
| default: | |
| break; | |
| } | |
| // Se define el numero de días de cada mes | |
| const daysInMonth: number[] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | |
| // Se validan años bisiestos | |
| if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) daysInMonth[1] = 29; | |
| if (month < 1 || month > 12 || day < 1 || day > daysInMonth[month - 1]) return false; | |
| return true; | |
| }; | |
| const formatDate = (value: string, inputFormat: Format, outputFormat: Format = 'DDMMYYYY'): string => { | |
| if (!isValidDate(value, inputFormat)) return 'Invalid Date'; | |
| let day!: number; | |
| let month!: number; | |
| let year!: number; | |
| const dateArray: string[] = value.split(inputFormat === 'YYYYMMDD' ? '-' : '/'); | |
| switch (inputFormat) { | |
| case 'YYYYMMDD': | |
| day = parseInt(dateArray[2]); | |
| month = parseInt(dateArray[1]); | |
| year = parseInt(dateArray[0]); | |
| break; | |
| case 'MMDDYYYY': | |
| day = parseInt(dateArray[1]); | |
| month = parseInt(dateArray[0]); | |
| year = parseInt(dateArray[2]); | |
| break; | |
| case 'DDMMYYYY': | |
| day = parseInt(dateArray[0]); | |
| month = parseInt(dateArray[1]); | |
| year = parseInt(dateArray[2]); | |
| break; | |
| default: | |
| break; | |
| } | |
| const formattedDate = | |
| outputFormat === 'YYYYMMDD' | |
| ? `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}` | |
| : outputFormat === 'MMDDYYYY' | |
| ? `${String(month).padStart(2, '0')}/${String(day).padStart(2, '0')}/${year}` | |
| : `${String(day).padStart(2, '0')}/${String(month).padStart(2, '0')}/${year}`; | |
| return formattedDate; | |
| }; | |
| export { isValidDate, formatDate }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment