Reputation: 6206
I got a wired problem.
from datetime import datetime, timedelta
start_date = '2019-05-01'
end_date = '2020-04-30'
start_date = datetime.strptime(start_date, "%Y-%m-%d")
print(start_date)
new_start_date = (datetime.strptime(end_date, '%Y-%m-%d') - timedelta(days=360)).strftime('%Y-%m-%d')
print(new_start_date)
The return is
2019-05-01 00:00:00
2019-05-06
It looks like, the first "start_date" contains date and time, and the second "new_start_date" only has date. Why? How can I make change to let the first "start_date" return only date, no time?
Upvotes: 0
Views: 790
Reputation: 5746
strptime
returns a datetime
object. Documentation
classmethod datetime.strptime(date_string, format)
Return a datetime corresponding to date_string, parsed according to format.
Where as strftime
returns a string specified by your formatting string. Documentation
date.strftime(format)
Return a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values. For a complete list of formatting directives, see strftime() and strptime() Behavior.
In your example;
datetime.strptime(start_date, "%Y-%m-%d") #2019-05-01 00:00:00
However if you were to use strftime
to format this, it would proceed to remove the time;
datetime.strptime(start_date, "%Y-%m-%d").strftime('%Y-%m-%d')) #2019-05-01
Upvotes: 1
Reputation: 1
new_start_date
is not a date
, it is a string
. You can remove strftime
to get the datetime
object.
Upvotes: 0