79588544

Date: 2025-04-23 11:54:17
Score: 2.5
Natty:
Report link

I remember facing a similar problem! The strftime function doesn’t add leading zeros for years before 1000, so for the year "33", it just outputs "33" instead of "0033". The problem becomes more obvious when you try to parse the string back using strptime, because the %Y format expects a 4-digit year.

The datetime module in Python generally expects years to be at least 4 digits long, and doesn’t add zeros for years less than 1000.

You can manually add leading zeros to the year to avoid the parsing issue and ensure consistency. For example:

import datetime
old_date = datetime.date(year=33, month=3, day=28)
formatted_date = old_date.strftime("%Y-%m-%d")
padded_date = f"{int(formatted_date[:4]):04d}{formatted_date[4:]}"

This ensures the year is always 4 digits, and you can safely parse it later.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Me too answer (2.5): facing a similar problem
  • Low reputation (1):
Posted by: Viktor Sbruev