Home > OS >  datetime.strptime is not support to convert future date?
datetime.strptime is not support to convert future date?

Time:11-13

I had the following error in Nov 13th 2022

end = datetime.strptime("2022-11-16", "%Y-%d-%m")

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/_strptime.py", line 352, in _strptime
    raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: 6

but I didn't have the error in the following case,

date_value = datetime.strptime("2022-11-12", "%Y-%d-%m")

Well, strptime function of datetime module is not support to convert for future date? If no, let me know the way....please

CodePudding user response:

Future dates are supported by datetime.strptime. Here, you're trying to convert a date with 16 months which is leading to the error. I'm guessing you're looking for

datetime.strptime("2022-11-16", "%Y-%m-%d")

CodePudding user response:

datetime.strptime("2022-11-16", "%Y-%d-%m")

That format string is year-day-month, but you're passing a date string of year-month-day. So it tried to interpret 16 as the month.

Your second example happened to work because it had 12 for the month.

CodePudding user response:

Datetime strptime supports future date. We only have 12 Months but you are asking for 16th Month which doesn't exist.

datetime.strptime("2022-16-11", "%Y-%d-%m")
  • Related