TheAlgorithms/Python · error · ValueError
Date separator must be '-' or '/'
Error message
Date separator must be '-' or '/'
What it means
Raised by zeller(date_input) in maths/zellers_congruence.py when the character at position 2 (the separator between month and day) is not '-' or '/'. The parser is strictly positional: chars 2 and 5 must be separators, digits are taken from fixed offsets. This is the first-separator check, hit by inputs like '01^31-2010'.
Source
Thrown at maths/zellers_congruence.py:100
"6": "Saturday",
}
convert_datetime_days = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0}
# Validate
if not 0 < len(date_input) < 11:
raise ValueError("Must be 10 characters long")
# Get month
m: int = int(date_input[0] + date_input[1])
# Validate
if not 0 < m < 13:
raise ValueError("Month must be between 1 - 12")
sep_1: str = date_input[2]
# Validate
if sep_1 not in ["-", "/"]:
raise ValueError("Date separator must be '-' or '/'")
# Get day
d: int = int(date_input[3] + date_input[4])
# Validate
if not 0 < d < 32:
raise ValueError("Date must be between 1 - 31")
# Get second separator
sep_2: str = date_input[5]
# Validate
if sep_2 not in ["-", "/"]:
raise ValueError("Date separator must be '-' or '/'")
# Get year
y: int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9])
# Arbitrary year range
if not 45 < y < 8500:
raise ValueError(View on GitHub (pinned to f5988cc097)
Solutions
- Reformat the date with '-' separators before calling: s.replace('.', '-').replace('/', '-') after verifying overall shape.
- Best: rebuild the string from a parsed date via strftime('%m-%d-%Y') so separators are guaranteed correct.
- Pre-validate with a regex such as ^\d{2}[-/]\d{2}[-/]\d{4}$ and reject mismatches early.
Example fix
# before
zeller('01.31.2010') # '.' separator -> ValueError
# after
zeller('01.31.2010'.replace('.', '-')) # '01-31-2010' Defensive patterns
Strategy: validation
Validate before calling
import re
def has_valid_separators(s: str) -> bool:
return len(s) == 10 and s[2] in '-/' and s[5] in '-/'
normalized = re.sub(r'[.\s]+', '-', s) Try / catch
try:
day = zeller(s)
except ValueError as e:
if 'separator' in str(e):
day = zeller(re.sub(r'[. ]', '-', s))
else:
raise Prevention
- Only '-' and '/' are accepted at positions 2 and 5.
- Normalize locale separators before calling.
When it happens
Trigger: Calling zeller('01.31.2010'), zeller('01 31 2010') or zeller('01^31-2010'); dates produced with '.', ' ' or other separators; strings where a leading character shifted positions.
Common situations: Locale-specific separators (dots in European dates, dots/spaces in ISO-adjacent formats); copy-pasted dates with non-breaking spaces; downstream systems reformatting the string.
Related errors
- Must be 10 characters long
- Month must be between 1 - 12
- Date must be between 1 - 31
- Power cannot be negative in any electrical/electronics syste
- One and only one argument must be 0
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/96d84dedd8ab126e.
Report an issue: GitHub.