Comprehensive date validation regex patterns for various formats including ISO, US, European, and dotted formats
^\d{4}4-digit year-\d{2}2-digit month (01-12)-\d{2}$2-digit day (01-31)\d{4}-\d{2}-\d{2}Date part, identical to the ISO format above([01]\d|2[0-3])Hour (00-23):[0-5]\dMinute and second (00-59), each written twice(0[1-9]|1[0-2])Month (01-12)\/(0[1-9]|[12]\d|3[01])Day (01-31)\/(19|20)\d{2}Year (1900-2099)// ISO format YYYY-MM-DD
const isoRegex = /^\d{4}-\d{2}-\d{2}$/;
console.log(isoRegex.test("2024-01-15")); // true
// ISO datetime YYYY-MM-DD HH:mm:ss
const dtRegex = /^\d{4}-\d{2}-\d{2} ([01]\d|2[0-3]):[0-5]\d:[0-5]\d$/;
console.log(dtRegex.test("2024-01-15 14:30:00")); // true
// Compact format YYYYMMDD
const compactRegex = /^(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])$/;
console.log(compactRegex.test("20240115")); // true
// US format MM/DD/YYYY
const usRegex = /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/(19|20)\d{2}$/;
console.log(usRegex.test("01/15/2024")); // trueimport re
# ISO format
iso_pattern = r"^\d{4}-\d{2}-\d{2}$"
print(bool(re.match(iso_pattern, "2024-01-15")))
# ISO datetime YYYY-MM-DD HH:mm:ss
dt_pattern = r"^\d{4}-\d{2}-\d{2} ([01]\d|2[0-3]):[0-5]\d:[0-5]\d$"
print(bool(re.match(dt_pattern, "2024-01-15 14:30:00")))
# Compact format YYYYMMDD
compact_pattern = r"^(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])$"
print(bool(re.match(compact_pattern, "20240115")))
# US format
us_pattern = r"^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/(19|20)\d{2}$"
print(bool(re.match(us_pattern, "01/15/2024")))import java.util.regex.*;
public class DateRegex {
public static void main(String[] args) {
// ISO format
String isoRegex = "^\\d{4}-\\d{2}-\\d{2}$";
System.out.println(Pattern.matches(isoRegex, "2024-01-15"));
// ISO datetime YYYY-MM-DD HH:mm:ss
String dtRegex = "^\\d{4}-\\d{2}-\\d{2} ([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$";
System.out.println(Pattern.matches(dtRegex, "2024-01-15 14:30:00"));
// US format
String usRegex = "^(0[1-9]|1[0-2])\\/(0[1-9]|[12]\\d|3[01])\\/(19|20)\\d{2}$";
System.out.println(Pattern.matches(usRegex, "01/15/2024"));
}
}2024-01-15ISO Format2024-01-15 14:30:00ISO DateTime20240115Compact Format01/15/2024US Format15/01/2024EU Format15.01.2024Dotted Format2024/01/15Invalid32/01/2024Invalid DayValidate date format when users fill out forms to ensure the entered date matches the expected format and avoid data format errors.
// Form date validation
if (!dateRegex.test(userDate)) {
showError("Please enter correct date format");
}Validate and standardize date formats during bulk data import to ensure data consistency and usability.
// Data cleaning
const validDates = rawData.filter(item =>
dateRegex.test(item.date)
);Validate and convert date formats according to different regional standards in internationalized applications, supporting multiple date format standards.
// Internationalized date validation
const regex = locale === 'US' ? usDateRegex :
locale === 'EU' ? euDateRegex : isoDateRegex;Date validation usually fails due to format mismatch. Please ensure the date format exactly matches the regular expression, including separators, digits, and order.
Correct formats: 2024-01-15, 2024-01-15 14:30:00, 01/15/2024, 15.01.2024
Incorrect formats: 2024/1/15, 1-15-24, 15/1/2024
Regular expressions can only validate format, not whether the date actually exists (like February 30th). It's recommended to combine with JavaScript's Date object for complete validation.
// Complete date validation
if (dateRegex.test(dateStr)) {
const date = new Date(dateStr);
return !isNaN(date.getTime());
}Use different regular expressions based on user locale or region settings. You can detect user locale and apply the corresponding date format validation.
// Regional format handling
const getDateRegex = (locale) => {
switch(locale) {
case 'US': return usDateRegex;
case 'EU': return euDateRegex;
default: return isoDateRegex;
}
}