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.
JavaScript
x
21
21
1
from datetime import datetime
2
from datetime import timedelta
3
4
# taking input as the date
5
Begindatestring = "2020-10-11"
6
7
# carry out conversion between string
8
# to datetime object
9
Begindate = datetime.strptime(Begindatestring, "%Y-%m-%d")
10
11
# print begin date
12
print("Beginning date")
13
print(Begindate)
14
15
# calculating end date by adding 1 day
16
Enddate = Begindate + timedelta(days=1)
17
18
# printing end date
19
print("Ending date")
20
print(Enddate)
21
this code works but the output its gives me looks like this
JavaScript
1
5
1
Beginning date
2
2020-10-11 00:00:00
3
Ending date
4
2020-10-12 00:00:00
5
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
JavaScript
1
5
1
Beginning date
2
2020-10-11
3
Ending date
4
2020-10-12
5
It seems like there might be a simple solution but I can’t find it.
Advertisement
Answer
Try using:
JavaScript
1
2
1
print(Begindate.strftime("%Y-%m-%d"))
2
Check https://www.programiz.com/python-programming/datetime/strftime to learn more.