sorry in advance if this is dense. I am trying to find the days since I last posted a tweet. The problem I am running into is when the dates are different, e.g., today and yesterday, but not enough hours have passed to be a full "day."
# "created_at" is part of the Twitter API, returned as UTC time. The
# timedelta here is to account for the fact I am on the west coast, USA
lastTweetAt = result.created_at + timedelta(hours=-8)
# get local time
rightNow = datetime.now()
# subtract the two datetimes (which gives me a timedelta)
dt = rightNow - lastTweetAt
# print the number of days difference
print dt.days
The problem is that if I posted a tweet at say 5 PM yesterday and am running the script at 8 AM today, only 15 hours have passed, which is 0 days. But obviously I want to say it's been 1 day since my last tweet if it was yesterday. And a kludge of adding "+1" isn't going to help because if I have tweeted today, I want the result to be 0.
Is there a better approach than using timedelta to get the difference?
Solution provided by Matti Lyra
The answer is to call .date() on the datetimes so they are converted to coarser date objects (without timestamps). The correct code looks like:
# "created_at" is part of the Twitter API, returned as UTC time.
# the -8 timedelta is to account for me being on the west coast, USA
lastTweetAt = result.created_at + timedelta(hours=-8)
# get UTC time for right now
rightNow = datetime.now()
# truncate the datetimes to date objects (which have dates, but no timestamp)
# and subtract them (which gives me a timedelta)
dt = rightNow.date() - lastTweetAt.date()
# print the number of days difference
print dt.days
To get the number of days in a time delta, just use the timedelta. days .
To get a time difference in seconds, use the timedelta. total_seconds() methods. Multiply the total seconds by 1000 to get the time difference in milliseconds. Divide the seconds by 60 to get the difference in minutes.
You can also find the difference between two datetime objects to get a timedelta object and perform the comparison based on the positive or negative value returned by the timedelta. total_seconds() function. if seconds < 0: print('First date is less than the second date.
How about dealing just with the "date" part of your datetimes?
The part after outputing "0" in the following code:
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now() - datetime.timedelta(hours=20)
>>> (a-b).days
0
>>> b.date() - a.date()
datetime.timedelta(-1)
>>> (b.date() - a.date()).days
-1
You can use datetime.date()
to compare the two dates (note: NOT dates with times), this truncates the datetime
to have an resolution of days not hours.
...
# subtract the two datetimes (which gives me a timedelta)
dt = rightNow.date() - lastTweetAt.date()
...
The docs are always your friend
http://docs.python.org/2/library/datetime#datetime.datetime.date
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With