Created
March 6, 2025 18:58
-
-
Save Steellgold/497bb703dd11812963eda4975ebd330f to your computer and use it in GitHub Desktop.
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
import { dayJS } from "./day-js"; | |
export const completeDateIfNecessary = (dayInput: string): string => { | |
const dateRegex = /^\d{2}\/\d{2}\/\d{4}$/; | |
if (dateRegex.test(dayInput)) { | |
console.log("Format correct :", dayInput); | |
return dayInput; | |
} | |
const day = parseInt(dayInput, 10); | |
if (!isNaN(day) && day >= 1 && day <= 31) { | |
const currentYear = dayJS().year(); | |
const currentMonth = dayJS().month() + 1; | |
const formattedMonth = currentMonth < 10 ? `0${currentMonth}` : `${currentMonth}`; | |
const formattedDay = day < 10 ? `0${day}` : `${day}`; | |
const formattedDate = `${formattedDay}/${formattedMonth}/${currentYear}`; | |
console.log("Date formée :", formattedDate); | |
if (!dayJS(formattedDate, "DD/MM/YYYY", true).isValid()) { | |
console.error("Date invalide après parsing :", formattedDate); | |
return "Invalid Date"; | |
} | |
return formattedDate; | |
} | |
console.error("Date invalide (hors plage 1-31) :", dayInput); | |
return "Invalid Date"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment