Example:
9:43 - 17:27 - how many hours and minutes elapsed between those times ?
Here is one approach to get the number of total minutes:
from datetime import datetime
s = '9:30 - 14:00 ; 14:30 - 16:30'
sum(((b-a).total_seconds()/60 for x in s.split(' ; ')
for a,b in [list(map(lambda t: datetime.strptime(t, '%H:%M'), x.split(' - ')))]))
Output: 390.0
If you know that the time periods will never span midnight, then you could simply split the time strings with time.split(":") and do the math yourself with the hours and minutes.
However, the correct solution would be to import the datetime module and calculate the timedelta.
This example could be condensed. I intentionally made it verbose without knowing exactly how you're getting your inputs:
from datetime import datetime
times = [
"9:30",
"14:00",
"14:30",
"16:30"
]
#Just using today's date to fill in the values with assumption all times are on the same day.
year = 2022
month = 6
day = 9
date_times = []
for time in times:
split_time = time.split(":")
hour = split_time[0]
minutes = split_time[1]
timestamp = datetime.datetime.today(year=year, month=month, day=day, hour=hour, min=minutes)
date_times.append(timestamp)
total_seconds = 0
for i in range(1, len(date_times), 2):
delta = date_times[i] - date_times[i-1] # The timedelta object returned will have days, seconds, milliseconds
total_seconds += delta.days * 86400 + delta.seconds
hours = total_seconds // 3600 # Integer division
minutes = round((total_seconds % 3600) / 60) # Change depending on if you want to round to nearest, or always up or down.
I'm using the datetime.time.now() for the current time, i.e. I want to perform an operation that counts in the totals of the hours (e.g. 1h:45min - 0h:50min). I cannot convert the current time to the datetime.timedelta object.
There is no datetime.time.now() — you must mean datetime.now() which returns a datetime instance which has a year, month, and day as well as the time of day. If you want a different time on the same day you can use its attributes to construct one.
If you subtract two datetimes the result is a timedelta.
You can also subtract an arbitrary timedelta from a datetime (resulting in another datetime).
Note that timedelta instances only have the attributes days, seconds, and microseconds, so if you want to know how long they are in hours and minutes, you have to manually calculate them.
Here's an example of doing all of these things.
from datetime import datetime, timedelta
now = datetime.now() # Current time.
# Construct a time on the same day.
sunrise = datetime(now.year, now.month, now.day, hour=6, minute=58)
if sunrise > now: # Future
delta = sunrise - now
when = 'will be in'
ago = ''
else: # Past
delta = now - sunrise
when = 'happened'
ago = 'ago'
days = delta.days
seconds = delta.seconds
hours = delta.seconds//3600
minutes = (delta.seconds//60) % 60
print(f'sunrise {when} {hours} hours {minutes} minutes {ago}')
print(f'30 minutes before sunrise today is {sunrise - timedelta(minutes=30)}')
I think I've found it; I wanted to compare the current time with the sunrise and sunset that Python itself retrieved.
I've done it this way now (so the next one can do it too)
import datetime as dt
DTN = dt.datetime.now()
H = int(DTN .strftime("%H"))
M = int(DTN .strftime("%M"))
S = int(DTN .strftime("%S"))
t1 = dt.timedelta(hours= H, minutes= M, seconds=S)
t2 = dt.timedelta(hours= 1, minutes= 0, seconds=0)
if t1 > t2:
timeCal = t1-t2 }
elif t1<t2:
timeCal = t2-t1
else:
timeCal = t1+t2
print(timeCal)
actual time = 20:00:00
result: 19:00:00
I'm struggling with date objects in python.
I have the following data:
from datetime import datetime, timedelta
# date retrieved from a list
ini = [u'2016-01-01']
# transform the ini in a readable string
ini2 = ', '.join(map(str, ini))
# transform the string a date object
date_1 = datetime.strptime(ini2, "%Y-%m-%d")
# number that is the length of the date
l = 365.0
# adding l to ini2
final = date_1 + timedelta(days = l)
Now I'd need to split the whole interval (that is the period from date_1 to final) by an input number (e.g. ts = 4) and, given another input date (e.g. new_date = u'2016-05-19') check in which interval it is (in the example 19th of May is in t2 = 2).
I hope I made myself clear enough.
Thanks
I tried different approaches but none seems the right one.
This might help:
from datetime import datetime, timedelta
def which_interval(date0, delta, date1, n_intervals):
date0 = datetime.strptime(date0, '%Y-%m-%d')
delta = timedelta(days = delta)
date1 = datetime.strptime(date1, '%Y-%m-%d')
delta1 = date1 - date0
quadrile = int(((float(delta1.days) / delta.days) * n_intervals))
return quadrile
# Example: figure out which quarter August 1st is in
interval = which_interval(
'2016-01-01',
366,
'2016-08-01',
4)
print '2016-08-01 is in interval %d, Q%d'%(interval, interval+1)
Note that this function uses python indices so it will start at quarter 0 and end at quarter 3. If you want 1-based indices (so the answer will be 1, 2, 3, or 4) you would want to add 1 to the result.
the timedelta object supports division, so use floor division by a step and you will get an interval in range(ts)
new_date = datetime.strptime(u'2016-05-19', "%Y-%m-%d")
ts = 4
step = timedelta(days=l)/ts #divide by the number of steps
interval = (new_date - date_1)//step #get the number this interval is in
so for date_1 <= new_date < date_1 + step interval will be 0, for date_1+step<=new_date < date_1 + step*2 interval will be 1, etc.
This of course is using python style indices so to get the number starting from 1, add one:
interval = (new_date - date_1)//step + 1
EDIT: the functionality to divide timedelta objects was only added in python3, you would need to use the .total_seconds() method to do the calculation in python 2:
step = timedelta(days=l).total_seconds()/ts #divide by inteval
interval = (new_date - date_1).total_seconds()//step
You could calculate this using the seconds total of the intervals.
import math
from datetime import datetime, timedelta
l = 365.0
factor = 4
date_1 = datetime.strptime('2016-01-01', "%Y-%m-%d")
lookup_dt = datetime.strptime('2016-12-01', "%Y-%m-%d")
def get_interval_num(factor, start_dt, td, lookup_dt):
final = start_dt + td
interval = (final - start_dt).total_seconds()
subinterval = interval / factor
interval_2 = (lookup_dt - start_dt).total_seconds()
return int(math.ceil(interval_2 / subinterval))
num = get_interval_num(
factor=factor,
start_dt=date_1,
td= timedelta(days=l),
lookup_dt=lookup_dt
)
print("The interval number is: %s" % num)
Output would be:
The interval number is: 4
EDIT: clearified variable naming, extended code snippet
I am hoping to generate a range of timestamps between:
18:00 (EST) on October 6th, 2014
and the same time 400 seconds later with an interval size of 2.2 seconds.
Getting the start and end dates:
When I do the following:
start_time = datetime.datetime(year = 2014,
month = 10,
day = 6,
hour = 18,
tzinfo = pytz.timezone('US/Eastern'))
end_time = start_time + datetime.timedelta(seconds=400)
Something seems to fail:
start_time.isoformat() returns '2014-10-06T18:06:40-04:56'
end_time.isoformat() returns '2014-10-06T18:06:40-04:56'
note that the time-zone offset for both timestamps above are: -04:56 (4 hours and 56 minutes) even though EST is 5 hours behind UTC. ?
Getting the time range:
Moving forward, if I try to get a range of timestamps between these two dates every 2.2 seconds (i.e. 2200 ms):
ts = pd.date_range(start=start_time, end=end_time, freq='2200L')
I get:
> ts[0]
Timestamp('2014-10-06 18:56:00-0400', tz='US/Eastern', offset='2200L')
or in other words:
> ts[0].isoformat()
'2014-10-06T18:56:00-04:00'
which also does not make sense (note that the time is 18:56, even though I was asking to get a range between 18:00 and 18:06:40 (i.e. 400 seconds after 18:00)
I got tired of dealing with Python's awkward datetime implementation (particularly with respect to timezones), and have started using crsmithdev.com/arrow. A solution using this lib:
import arrow
start_time = arrow.get(2014, 10, 6, tzinfo='US/Eastern')
end_time = start_time.replace(seconds=400)
print start_time.isoformat()
print end_time.isoformat()
# alternate form
start_time = arrow.get('2014-10-06T18:00:00.000-04:00')
end_time = start_time.replace(seconds=400)
print start_time.isoformat()
print end_time.isoformat()
# get a datetime from an arrow object
start_time_dt = start_time.datetime
import dateutil.parser as parser
import datetime
start_time = parser.parse("06/Oct/2015 18:00 EST")
end_time = start_time + datetime.timedelta(seconds=400)
interval = datetime.timedelta(seconds=2.2)
current_time = start_time
while current_time < end_time:
current_time += interval
print current_time
but I probably dont understand what your issue is
I have two times, a start and a stop time, in the format of 10:33:26 (HH:MM:SS). I need the difference between the two times. I've been looking through documentation for Python and searching online and I would imagine it would have something to do with the datetime and/or time modules. I can't get it to work properly and keep finding only how to do this when a date is involved.
Ultimately, I need to calculate the averages of multiple time durations. I got the time differences to work and I'm storing them in a list. I now need to calculate the average. I'm using regular expressions to parse out the original times and then doing the differences.
For the averaging, should I convert to seconds and then average?
Yes, definitely datetime is what you need here. Specifically, the datetime.strptime() method, which parses a string into a datetime object.
from datetime import datetime
s1 = '10:33:26'
s2 = '11:15:49' # for example
FMT = '%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
That gets you a timedelta object that contains the difference between the two times. You can do whatever you want with that, e.g. converting it to seconds or adding it to another datetime.
This will return a negative result if the end time is earlier than the start time, for example s1 = 12:00:00 and s2 = 05:00:00. If you want the code to assume the interval crosses midnight in this case (i.e. it should assume the end time is never earlier than the start time), you can add the following lines to the above code:
if tdelta.days < 0:
tdelta = timedelta(
days=0,
seconds=tdelta.seconds,
microseconds=tdelta.microseconds
)
(of course you need to include from datetime import timedelta somewhere). Thanks to J.F. Sebastian for pointing out this use case.
Try this -- it's efficient for timing short-term events. If something takes more than an hour, then the final display probably will want some friendly formatting.
import time
start = time.time()
time.sleep(10) # or do something more productive
done = time.time()
elapsed = done - start
print(elapsed)
The time difference is returned as the number of elapsed seconds.
Here's a solution that supports finding the difference even if the end time is less than the start time (over midnight interval) such as 23:55:00-00:25:00 (a half an hour duration):
#!/usr/bin/env python
from datetime import datetime, time as datetime_time, timedelta
def time_diff(start, end):
if isinstance(start, datetime_time): # convert to datetime
assert isinstance(end, datetime_time)
start, end = [datetime.combine(datetime.min, t) for t in [start, end]]
if start <= end: # e.g., 10:33:26-11:15:49
return end - start
else: # end < start e.g., 23:55:00-00:25:00
end += timedelta(1) # +day
assert end > start
return end - start
for time_range in ['10:33:26-11:15:49', '23:55:00-00:25:00']:
s, e = [datetime.strptime(t, '%H:%M:%S') for t in time_range.split('-')]
print(time_diff(s, e))
assert time_diff(s, e) == time_diff(s.time(), e.time())
Output
0:42:23
0:30:00
time_diff() returns a timedelta object that you can pass (as a part of the sequence) to a mean() function directly e.g.:
#!/usr/bin/env python
from datetime import timedelta
def mean(data, start=timedelta(0)):
"""Find arithmetic average."""
return sum(data, start) / len(data)
data = [timedelta(minutes=42, seconds=23), # 0:42:23
timedelta(minutes=30)] # 0:30:00
print(repr(mean(data)))
# -> datetime.timedelta(0, 2171, 500000) # days, seconds, microseconds
The mean() result is also timedelta() object that you can convert to seconds (td.total_seconds() method (since Python 2.7)), hours (td / timedelta(hours=1) (Python 3)), etc.
This site says to try:
import datetime as dt
start="09:35:23"
end="10:23:00"
start_dt = dt.datetime.strptime(start, '%H:%M:%S')
end_dt = dt.datetime.strptime(end, '%H:%M:%S')
diff = (end_dt - start_dt)
diff.seconds/60
This forum uses time.mktime()
Structure that represent time difference in Python is called timedelta. If you have start_time and end_time as datetime types you can calculate the difference using - operator like:
diff = end_time - start_time
you should do this before converting to particualr string format (eg. before start_time.strftime(...)). In case you have already string representation you need to convert it back to time/datetime by using strptime method.
I like how this guy does it — https://amalgjose.com/2015/02/19/python-code-for-calculating-the-difference-between-two-time-stamps.
Not sure if it has some cons.
But looks neat for me :)
from datetime import datetime
from dateutil.relativedelta import relativedelta
t_a = datetime.now()
t_b = datetime.now()
def diff(t_a, t_b):
t_diff = relativedelta(t_b, t_a) # later/end time comes first!
return '{h}h {m}m {s}s'.format(h=t_diff.hours, m=t_diff.minutes, s=t_diff.seconds)
Regarding to the question you still need to use datetime.strptime() as others said earlier.
Try this
import datetime
import time
start_time = datetime.datetime.now().time().strftime('%H:%M:%S')
time.sleep(5)
end_time = datetime.datetime.now().time().strftime('%H:%M:%S')
total_time=(datetime.datetime.strptime(end_time,'%H:%M:%S') - datetime.datetime.strptime(start_time,'%H:%M:%S'))
print total_time
OUTPUT :
0:00:05
import datetime as dt
from dateutil.relativedelta import relativedelta
start = "09:35:23"
end = "10:23:00"
start_dt = dt.datetime.strptime(start, "%H:%M:%S")
end_dt = dt.datetime.strptime(end, "%H:%M:%S")
timedelta_obj = relativedelta(start_dt, end_dt)
print(
timedelta_obj.years,
timedelta_obj.months,
timedelta_obj.days,
timedelta_obj.hours,
timedelta_obj.minutes,
timedelta_obj.seconds,
)
result:
0 0 0 0 -47 -37
Both time and datetime have a date component.
Normally if you are just dealing with the time part you'd supply a default date. If you are just interested in the difference and know that both times are on the same day then construct a datetime for each with the day set to today and subtract the start from the stop time to get the interval (timedelta).
Take a look at the datetime module and the timedelta objects. You should end up constructing a datetime object for the start and stop times, and when you subtract them, you get a timedelta.
you can use pendulum:
import pendulum
t1 = pendulum.parse("10:33:26")
t2 = pendulum.parse("10:43:36")
period = t2 - t1
print(period.seconds)
would output:
610
import datetime
day = int(input("day[1,2,3,..31]: "))
month = int(input("Month[1,2,3,...12]: "))
year = int(input("year[0~2020]: "))
start_date = datetime.date(year, month, day)
day = int(input("day[1,2,3,..31]: "))
month = int(input("Month[1,2,3,...12]: "))
year = int(input("year[0~2020]: "))
end_date = datetime.date(year, month, day)
time_difference = end_date - start_date
age = time_difference.days
print("Total days: " + str(age))
Concise if you are just interested in the time elapsed that is under 24 hours. You can format the output as needed in the return statement :
import datetime
def elapsed_interval(start,end):
elapsed = end - start
min,secs=divmod(elapsed.days * 86400 + elapsed.seconds, 60)
hour, minutes = divmod(min, 60)
return '%.2d:%.2d:%.2d' % (hour,minutes,secs)
if __name__ == '__main__':
time_start=datetime.datetime.now()
""" do your process """
time_end=datetime.datetime.now()
total_time=elapsed_interval(time_start,time_end)
Usually, you have more than one case to deal with and perhaps have it in a pd.DataFrame(data) format. Then:
import pandas as pd
df['duration'] = pd.to_datetime(df['stop time']) - pd.to_datetime(df['start time'])
gives you the time difference without any manual conversion.
Taken from Convert DataFrame column type from string to datetime.
If you are lazy and do not mind the overhead of pandas, then you could do this even for just one entry.
Here is the code if the string contains days also [-1 day 32:43:02]:
print(
(int(time.replace('-', '').split(' ')[0]) * 24) * 60
+ (int(time.split(' ')[-1].split(':')[0]) * 60)
+ int(time.split(' ')[-1].split(':')[1])
)