Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how set Month as string in Python datetime

I found this example how to set time and covert back to readable form

import datetime

x = datetime.datetime(2020, 5, 17)
print(x.strftime("%Y %b %d"))

My question is how to set new date with Month as string ?
Is it possible ? Is there some parameter/function for this ?

y = datetime.datetime(2020, May, 17)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is no function provided which accepts a string argument to specify the month. But you can use the strptime class method to parse a string that contains a month name into a datetime value.

>>> from datetime import datetime
>>> datetime.strptime("2020 May 17", "%Y %b %d")
datetime.datetime(2020, 5, 17, 0, 0)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading