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 can I strip the 00:00:00 from a date in python

This might be a really simple question, but I’m using the code below to add 1 day to a date and then output the new date. I found this code online.

from datetime import datetime
from datetime import timedelta
  
# taking input as the date
Begindatestring = "2020-10-11"
  
# carry out conversion between string 
# to datetime object
Begindate = datetime.strptime(Begindatestring, "%Y-%m-%d")
  
# print begin date
print("Beginning date")
print(Begindate)
  
# calculating end date by adding 1 day
Enddate = Begindate + timedelta(days=1)
  
# printing end date
print("Ending date")
print(Enddate)

this code works but the output its gives me looks like this

Beginning date
2020-10-11 00:00:00
Ending date
2020-10-12 00:00:00

but for the rest of my code to run properly I need to get rid of the 00:00:00
so I need an output that looks like this

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

    Beginning date
    2020-10-11
    Ending date
    2020-10-12

It seems like there might be a simple solution but I can’t find it.

>Solution :

Try using:

print(Begindate.strftime("%Y-%m-%d"))

Check https://www.programiz.com/python-programming/datetime/strftime to learn more.

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