[Python] datetime.datetime rejects 29 February

Discussão em 'Python' iniciado por Stack, Novembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I am using Python 3.11 on Windows 11 I get the following result:

    d = 'Mon 29 Feb'
    import datetime
    datetime.datetime.strptime(d, '%a %d %b')


    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in <module>
    datetime.datetime.strptime(d, '%a %d %b')
    File "C:\Users\pskipwith\AppData\Local\Programs\Python\Python311\Lib\_strptime.py", line 579, in _strptime_datetime
    return cls(*args)
    ValueError: day is out of range for month


    but if I add the year, say 2016, it goes through without error:

    d = 'Mon 29 Feb 2016'
    datetime.datetime.strptime(d, '%a %d %b %Y')
    datetime.datetime(2016, 2, 29, 0, 0)


    Can this be correct behaviour?

    Continue reading...

Compartilhe esta Página