Amitash
Amitash

Reputation: 1029

Python subtracting two date strings

I have two strings containing a date like so"

start_date = 'Sun Sep 16 16:05:15 +0000 2012'
end_date = 'Sun Sep 17 23:55:20 +0000 2012'

I need to perform: end_date - start_date It should return the number of seconds separating the end and start dates.

This data is extracted from the twitter api. This is what the json gives me. Since it seems like a commonly used string, I assume there's a library or method that can handle this. I'm just not able to find one. Thanks!

Upvotes: 9

Views: 34269

Answers (7)

tanuki505
tanuki505

Reputation: 23

from datetime import datetime

start_date = 'Sun Sep 16 16:05:15 +0000 2012'
end_date = 'Sun Sep 17 23:55:20 +0000 2012'


def __datetime(date_str):
    return datetime.strptime(date_str, '%a %b %d %H:%M:%S +0000 %Y')

start = __datetime(start_date)
end = __datetime(end_date)

delta = end - start
print(delta)
print(delta.total_seconds())

hours = (delta.total_seconds())/3600

print("Hours %s" % hours)

minutes = (delta.total_seconds())//3600

print("minutes %s" % minutes)
print(divmod(delta.total_seconds(), 3600))

h, m = divmod(delta.total_seconds(), 3600)

print(h)
print(m)

M, s = divmod(m, 60)
print(M)
print(s)

Upvotes: 1

sphere
sphere

Reputation: 1350

Here is the full answer:

from datetime import datetime

start_date = 'Sun Sep 16 16:05:15 +0000 2012'
end_date = 'Sun Sep 17 23:55:20 +0000 2012'


def __datetime(date_str):
    return datetime.strptime(date_str, '%a %b %d %H:%M:%S +0000 %Y')

start = __datetime(start_date)
end = __datetime(end_date)

delta = end - start
print delta  # prints: 1 day, 7:50:05
print delta.total_seconds()  # prints: 114605.0

Upvotes: 19

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29093

use datetime.strptime method to convert your string to datetime object. You can find required formats here: http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

In your case:

%a Locale’s abbreviated weekday name.
%b Locale’s abbreviated month name.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%Y Year with century as a decimal number.
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).

Upvotes: 2

unwind
unwind

Reputation: 399703

How hard did you look?

Python's datetime module will parse this format, and let you do the arithmetic on the resulting objects.

Here's sample code:

a = datetime.datetime.strptime("Sun Sep 16 16:05:15 +0000 2012", "%a %b %d %H:%M:%S +0000 %Y")

Note that in Windows (where I tried the above), the %z directive seems to not be supported, which is why the +0000 part is hard-coded. If you expect this part (the UTC offset) to vary, you'll need to handle that in a separate step, unless you can verify that %z works for you.

Upvotes: 4

japreiss
japreiss

Reputation: 11251

Use datetime.strptime() to parse the string according to a format code. In your case the format would be something like "%a %b %d %H:%M:%S %z %Y". (note: I did not test this parse format.)

Details: http://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

Then you can subtract datetime objects and get a timedelta object.

Or, you could use one of the Python Twitter API wrappers listed on Twitter's developer page, which probably do the conversion for you: https://dev.twitter.com/docs/twitter-libraries#python

Upvotes: 2

John Percival Hackworth
John Percival Hackworth

Reputation: 11531

You can parse the dates using datetime.datetime.strptime(), then you can do arithmetic manipulations?

Upvotes: 1

Paolo Casciello
Paolo Casciello

Reputation: 8202

View the datetime module ( http://docs.python.org/2/library/datetime.html ) It has methods for date manipulation of every kind

Upvotes: 2

Related Questions