Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support


📝 Hey there! 👋 Are you struggling to validate date formats like "dd/mm/YYYY" with leap year support using regular expressions? Don't worry! In this blog post, I've got you covered with an easy and foolproof solution. So, let's dive right in and solve this problem together! 😄
🔍 The Problem: So, you have a date string, and you want to validate it using a regular expression. Although you already have a regex that validates "dd/mm/yyyy," it fails to handle invalid dates such as "31/02/4500." Now you're wondering, "What is a valid regex to validate 'dd/mm/yyyy' format with leap year support?" 🤔
💡 The Solution: To validate "dd/mm/yyyy" format with leap year support, we need to consider a few things:
1️⃣ Leap Year: A leap year occurs every four years, except for years divisible by 100 (unless they are divisible by 400). In simple terms, a leap year has 366 days instead of the usual 365.
2️⃣ Month-Specific Days: Each month has a specific number of days, which should be considered while validating the date.
With these considerations in mind, we can come up with a regex that handles all the edge cases for validating "dd/mm/yyyy" format with leap year support: ✨
^(0?[1-9]|[12][0-9]|3[01])[./ -](0?[1-9]|1[012])[./ -](?:(?:19|20)[0-9]{2}|(?:2[0-9](?:0[48]|[2468][048]|[13579][26])|(?:19|20)(?:0[48]|[2468][048]|[13579][26])|2000))$
This regular expression covers the following formats:
1️⃣ dd/mm/yyyy 2️⃣ dd-mm-yyyy 3️⃣ dd.mm.yyyy 4️⃣ dd mmm yyyy 5️⃣ dd-mmm-yyyy 6️⃣ dd/mmm/yyyy 7️⃣ dd.mmm.yyyy
🔮 How does it work? Let me break it down for you:
(0?[1-9]|[12][0-9]|3[01])
matches the day part (1 to 31), allowing single-digit days, ensuring it's within a valid range.[./ -]
matches any separator character like ".", "/", "-", or a space.(0?[1-9]|1[012])
matches the month part (1 to 12), allowing single-digit months.(?:(?:19|20)[0-9]{2}|(?:2[0-9](?:0[48]|[2468][048]|[13579][26])|(?:19|20)(?:0[48]|[2468][048]|[13579][26])|2000))
matches the year part (either 19xx, 20xx, or a leap year).
👍 This regex validates both the correct date formats and leap year support in a single expression. However, it doesn't validate the month-specific number of days (e.g., 30th February). For that, you'll need additional code or logic.
And that's it, my friend! You now have a regex that validates "dd/mm/yyyy" format with leap year support. 🎉
📢 Take Action: Now that you've mastered this regex, try implementing it in your code and see how it performs. If you encounter any issues or have any further questions, feel free to leave a comment below. Your feedback and engagement are highly appreciated! Let's conquer these date validation challenges together! 💪🗓️
Happy regexing! ✨😄
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
