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

Strptime() function won't give full python datetime

I have the following datetime which I’m trying to convert into a datetime object.

BEGINDATE = '20211121 09:00:00.000' 
start_date = datetime.strptime(BEGINDATE, "%Y%m%d %H:%M:%S.%f")

I expect to see an output like when I run datetime.now(), i.e.

datetime.datetime(2022, 4, 11, 9, 28, 45, 447007)

Instead, I get the following

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

datetime.datetime(2021, 11, 21, 9, 0)

It cuts out two of the time arguments and also displays the minutes as 0 rather than 00. Have I formatted it wrong?

>Solution :

I you try with non zero values like this:

BEGINDATE = '20211121 09:01:02.120' 
start_date = datetime.strptime(BEGINDATE, "%Y%m%d %H:%M:%S.%f")

you get:

 datetime.datetime(2021, 11, 21, 9, 1, 2, 120000)

so the the script is correct and the problem is in the data.

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