Reputation: 69
I have this function where it calculates today. I am wondering why is the following results are happening.
today = datetime.datetime.now()
shows as 2018-06-13 17:13:42.372469
today = datetime.datetime.now().date()
shows as 2018-06-13
but when I try to use timedelta like this:
today = datetime.datetime.now().date() + datetime.timedelta(hours=-8)
it shows 2018-06-12. A full day back instead of 8 hours (should show 2018-06-13 9:13:42)
Can someone please explain why this happens and have timedelta correctly go back 8 hours instead of a day?
Upvotes: 0
Views: 96
Reputation: 336078
By reducing the timestamp to the date only, you have effectively set the time to 00:00:00 - substracting 8 hours from 2018-06-13 midnight correctly results in 2018-06-12.
Why not use the correct timestamps for calculation, and only convert them to dates after you have performed them?
Upvotes: 3