🔍Regex Recipes

ISO Date Format (YYYY-MM-DD)

Validate ISO 8601 date format (YYYY-MM-DD). Note: validates format only, not actual date validity.

Pattern

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Explanation

Matches dates in YYYY-MM-DD format with basic month (01-12) and day (01-31) validation.

Examples

Valid date
Input
2024-12-17
Output
✓ Match
Valid date
Input
2024-01-01
Output
✓ Match
Invalid - wrong format
Input
17-12-2024
Output
✗ No match
Invalid - single digit
Input
2024-1-1
Output
✗ No match
Format valid, date invalid
Input
2024-02-31
Output
✓ Match (but invalid date!)

Code Examples

JavaScript
const isoDateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;

// Validate format
const hasValidFormat = isoDateRegex.test('2024-12-17');

// Also validate actual date
function isValidDate(dateString) {
  if (!isoDateRegex.test(dateString)) return false;
  const date = new Date(dateString);
  return date.toISOString().startsWith(dateString);
}

Try it Now

💡 Tips

  • Always validate actual date validity after regex check
  • Use Date object or date library for complete validation
  • Consider adding time/timezone if needed
  • For user input, be flexible with format and normalize

⚠️ Common Pitfalls

  • Does not validate if date actually exists (e.g., Feb 31)
  • Does not check leap years
  • Does not include time or timezone information
  • Will match 2024-02-31 even though it's invalid