How do I calculate the difference in time in minutes for the following timestamp in Python?
2010-01-01 17:31:22
2010-01-03 17:31:22
minutes_diff = (datetime_end - datetime_start).total_seconds() / 60.0
RSabet's answer doesn't work in cases where the dates don't have the same exact time.
Original problem:
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 17:31:22', fmt)
daysDiff = (d2-d1).days
print daysDiff
> 2
# Convert days to minutes
minutesDiff = daysDiff * 24 * 60
print minutesDiff
> 2880
d2-d1 gives you a datetime.timedelta and when you use days it will only show you the days in the timedelta. In this case it works fine, but if you would have the following.
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 16:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
daysDiff = (d2-d1).days
print daysDiff
> 2
# Convert days to minutes
minutesDiff = daysDiff * 24 * 60
print minutesDiff
> 2880 # that is wrong
It would have still given you the same answer since it still returns 2 for days; it ignores the hour, min and second from the timedelta.
A better approach would be to convert the dates to a common format and then do the calculation. The easiest way to do this is to convert them to Unix timestamps. Here is the code to do that.
from datetime import datetime
import time
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
# Convert to Unix timestamp
d1_ts = time.mktime(d1.timetuple())
d2_ts = time.mktime(d2.timetuple())
# They are now in seconds, subtract and then divide by 60 to get minutes.
print int(d2_ts-d1_ts) / 60
> 3043 # Much better
In case someone doesn't realize it, one way to do this would be to combine Christophe and RSabet's answers:
from datetime import datetime
import time
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
diff = d2 -d1
diff_minutes = (diff.days * 24 * 60) + (diff.seconds/60)
print(diff_minutes)
> 3043
To calculate with a different time date:
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 16:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
diff = d2-d1
diff_minutes = diff.seconds/60
Use datetime.strptime() to parse into datetime instances, and then compute the difference, and finally convert the difference into minutes.
The result depends on the timezone that corresponds to the input time strings.
The simplest case if both dates use the same utc offset:
#!/usr/bin/env python3
from datetime import datetime, timedelta
time_format = "%Y-%d-%m %H:%M:%S"
dt1 = datetime.strptime("2010-01-01 17:31:22", time_format)
dt2 = datetime.strptime("2010-01-03 17:31:22", time_format)
print((dt2 - dt1) // timedelta(minutes=1)) # minutes
If your Python version doesn't support td // timedelta; replace it with int(td.total_seconds() // 60).
If the input time is in the local timezone that might have different utc offset at different times e.g., it has daylight saving time then you should make dt1, dt2 into aware datetime objects before finding the difference, to take into account the possible changes in the utc offset.
The portable way to make an aware local datetime objects is to use pytz timezones:
#!/usr/bin/env python
from datetime import timedelta
import tzlocal # $ pip install tzlocal
local_tz = tzlocal.get_localzone() # get pytz timezone
aware_dt1, aware_dt2 = map(local_tz.localize, [dt1, dt2])
td = aware_dt2 - aware_dt1 # elapsed time
If either dt1 or dt2 correspond to an ambiguous time then the default is_dst=False is used to disambiguate. You could set is_dst=None to raise an exception for ambiguous or non-existent local times instead.
If you can't install 3rd party modules then time.mktime() could be used from #Ken Cochrane's answer that can find the correct utc offset on some platforms for some dates in some timezones -- if you don't need a consistent (but perhaps wrong) result then it is much better than doing dt2 - dt1 with naive datetime objects that always fails if the corresponding utc offsets are different.
If you are trying to find the difference between timestamps that are in pandas columns, the the answer is fairly simple.
If you need it in days or seconds then
# For difference in days:
df['diff_in_days']=(df['timestamp2'] - df['timestamp1']).dt.days
# For difference in seconds
df['diff_in_seconds']=(df['timestamp2'] - df['timestamp1']).dt.seconds
Now minute is tricky as dt.minute works only on datetime64[ns] dtype.
whereas the column generated from subtracting two datetimes has format
AttributeError: 'TimedeltaProperties' object has no attribute 'm8'
So like mentioned by many above to get the actual value of the difference in minute you have to do:
df['diff_in_min']=df['diff_in_seconds']/60
But if just want the difference between the minute parts of the two timestamps then do the following
#convert the timedelta to datetime and then extract minute
df['diff_in_min']=(pd.to_datetime(df['timestamp2']-df['timestamp1'])).dt.minute
You can also read the article https://docs.python.org/3.4/library/datetime.html
and see section 8.1.2 you'll see the read only attributes are only seconds,days and milliseconds. And this settles why the minute function doesn't work directly.
In Other ways to get difference between date;
import dateutil.parser
import datetime
timeDifference = current_date - dateutil.parser.parse(last_sent_date)
time_difference_in_minutes = (int(timeDifference.days) * 24 * 60) + int((timeDifference.seconds) / 60)
Thanks
As was kind of said already, you need to use datetime.datetime's strptime method:
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 17:31:22', fmt)
daysDiff = (d2-d1).days
# convert days to minutes
minutesDiff = daysDiff * 24 * 60
print minutesDiff
there is also a sneak way with pandas:
pd.to_timedelta(x) - pd.to_timedelta(y)
You can solve it using divmod,
minutes = divmod((end_date - start_date).total_seconds(), 60)[0]
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 17:31:22', fmt)
print (d2-d1).days * 24 * 60
Related
How do I calculate the difference in time in minutes for the following timestamp in Python?
2010-01-01 17:31:22
2010-01-03 17:31:22
minutes_diff = (datetime_end - datetime_start).total_seconds() / 60.0
RSabet's answer doesn't work in cases where the dates don't have the same exact time.
Original problem:
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 17:31:22', fmt)
daysDiff = (d2-d1).days
print daysDiff
> 2
# Convert days to minutes
minutesDiff = daysDiff * 24 * 60
print minutesDiff
> 2880
d2-d1 gives you a datetime.timedelta and when you use days it will only show you the days in the timedelta. In this case it works fine, but if you would have the following.
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 16:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
daysDiff = (d2-d1).days
print daysDiff
> 2
# Convert days to minutes
minutesDiff = daysDiff * 24 * 60
print minutesDiff
> 2880 # that is wrong
It would have still given you the same answer since it still returns 2 for days; it ignores the hour, min and second from the timedelta.
A better approach would be to convert the dates to a common format and then do the calculation. The easiest way to do this is to convert them to Unix timestamps. Here is the code to do that.
from datetime import datetime
import time
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
# Convert to Unix timestamp
d1_ts = time.mktime(d1.timetuple())
d2_ts = time.mktime(d2.timetuple())
# They are now in seconds, subtract and then divide by 60 to get minutes.
print int(d2_ts-d1_ts) / 60
> 3043 # Much better
In case someone doesn't realize it, one way to do this would be to combine Christophe and RSabet's answers:
from datetime import datetime
import time
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
diff = d2 -d1
diff_minutes = (diff.days * 24 * 60) + (diff.seconds/60)
print(diff_minutes)
> 3043
To calculate with a different time date:
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 16:31:22', fmt)
d2 = datetime.strptime('2010-01-03 20:15:14', fmt)
diff = d2-d1
diff_minutes = diff.seconds/60
Use datetime.strptime() to parse into datetime instances, and then compute the difference, and finally convert the difference into minutes.
The result depends on the timezone that corresponds to the input time strings.
The simplest case if both dates use the same utc offset:
#!/usr/bin/env python3
from datetime import datetime, timedelta
time_format = "%Y-%d-%m %H:%M:%S"
dt1 = datetime.strptime("2010-01-01 17:31:22", time_format)
dt2 = datetime.strptime("2010-01-03 17:31:22", time_format)
print((dt2 - dt1) // timedelta(minutes=1)) # minutes
If your Python version doesn't support td // timedelta; replace it with int(td.total_seconds() // 60).
If the input time is in the local timezone that might have different utc offset at different times e.g., it has daylight saving time then you should make dt1, dt2 into aware datetime objects before finding the difference, to take into account the possible changes in the utc offset.
The portable way to make an aware local datetime objects is to use pytz timezones:
#!/usr/bin/env python
from datetime import timedelta
import tzlocal # $ pip install tzlocal
local_tz = tzlocal.get_localzone() # get pytz timezone
aware_dt1, aware_dt2 = map(local_tz.localize, [dt1, dt2])
td = aware_dt2 - aware_dt1 # elapsed time
If either dt1 or dt2 correspond to an ambiguous time then the default is_dst=False is used to disambiguate. You could set is_dst=None to raise an exception for ambiguous or non-existent local times instead.
If you can't install 3rd party modules then time.mktime() could be used from #Ken Cochrane's answer that can find the correct utc offset on some platforms for some dates in some timezones -- if you don't need a consistent (but perhaps wrong) result then it is much better than doing dt2 - dt1 with naive datetime objects that always fails if the corresponding utc offsets are different.
If you are trying to find the difference between timestamps that are in pandas columns, the the answer is fairly simple.
If you need it in days or seconds then
# For difference in days:
df['diff_in_days']=(df['timestamp2'] - df['timestamp1']).dt.days
# For difference in seconds
df['diff_in_seconds']=(df['timestamp2'] - df['timestamp1']).dt.seconds
Now minute is tricky as dt.minute works only on datetime64[ns] dtype.
whereas the column generated from subtracting two datetimes has format
AttributeError: 'TimedeltaProperties' object has no attribute 'm8'
So like mentioned by many above to get the actual value of the difference in minute you have to do:
df['diff_in_min']=df['diff_in_seconds']/60
But if just want the difference between the minute parts of the two timestamps then do the following
#convert the timedelta to datetime and then extract minute
df['diff_in_min']=(pd.to_datetime(df['timestamp2']-df['timestamp1'])).dt.minute
You can also read the article https://docs.python.org/3.4/library/datetime.html
and see section 8.1.2 you'll see the read only attributes are only seconds,days and milliseconds. And this settles why the minute function doesn't work directly.
In Other ways to get difference between date;
import dateutil.parser
import datetime
timeDifference = current_date - dateutil.parser.parse(last_sent_date)
time_difference_in_minutes = (int(timeDifference.days) * 24 * 60) + int((timeDifference.seconds) / 60)
Thanks
As was kind of said already, you need to use datetime.datetime's strptime method:
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 17:31:22', fmt)
daysDiff = (d2-d1).days
# convert days to minutes
minutesDiff = daysDiff * 24 * 60
print minutesDiff
there is also a sneak way with pandas:
pd.to_timedelta(x) - pd.to_timedelta(y)
You can solve it using divmod,
minutes = divmod((end_date - start_date).total_seconds(), 60)[0]
from datetime import datetime
fmt = '%Y-%m-%d %H:%M:%S'
d1 = datetime.strptime('2010-01-01 17:31:22', fmt)
d2 = datetime.strptime('2010-01-03 17:31:22', fmt)
print (d2-d1).days * 24 * 60
Question How do I print out the remaining hours of overtime
Example: I have to be at work for 8 hours, and if my time goes over 8 hours as shown in OUTPUT then I just wanna have the 00:03:00 printed out..
Meaning that I have 3 min overtime that day.
from datetime import datetime
s1 = '07:15:00'
s2 = '16:18:00'
FMT = '%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
print(tdelta)
OUTPUT
9:03:00
Subtract the length of the working day with a timedelta object...
from datetime import datetime, timedelta
s1 = '07:15:00'
s2 = '16:18:00'
FMT = '%H:%M:%S'
work_time = timedelta(hours=8)
lunch_time = timedelta(hours=1)
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT) - work_time - lunch_time
print(tdelta)
Output:
0:03:00
You need to subtract the length of the working day (and it would appear an hour to account for breaks) from tdelta to determine the excess.
In my task what I need to do is to subtract some hours and minutes like (0:20,1:10...any value) from time (2:34 PM) and on the output side I need to display the time after subtraction.
time and hh:mm value are hardcoded
Ex:
my_time = 1:05 AM
duration = 0:50
so output should be 12:15 PM
Output should be exact and in AM/PM Format and Applicable for all values
(0:00 < Duration > 6:00) .
I don't care about seconds, I only need the hours and minutes.
from datetime import datetime, timedelta
d = datetime.today() - timedelta(hours=0, minutes=50)
d.strftime('%H:%M %p')
This worked for me :
from datetime import datetime
s1 = '10:04:00'
s2 = '11:03:11' # for example
format = '%H:%M:%S'
time = datetime.strptime(s2, format) - datetime.strptime(s1, format)
print time
from datetime import datetime
d1 = datetime.strptime("01:05:00.000", "%H:%M:%S.%f")
d2 = datetime.strptime("00:50:00.000", "%H:%M:%S.%f")
print (d1-d2)
Seems like this should be so simple but for the life of me, I can't find the answer. I pull two datetimes/timestamps from the database:
2015-08-10 19:33:27.653
2015-08-10 19:31:28.209
How do I subtract the first from the second, preferably the result being in milliseconds? And yes, I have the date in there, too, because I need it to work at around midnight, as well.
Parse your strings as datetime.datetime objects and subtract them:
from datetime import datetime
d1 = datetime.strptime("2015-08-10 19:33:27.653", "%Y-%m-%d %H:%M:%S.%f")
d2 = datetime.strptime("2015-08-10 19:31:28.209", "%Y-%m-%d %H:%M:%S.%f")
print(d1 - d2)
Gives me:
0:01:59.444000
Also check out timedelta documentation for all possible operations.
you can do subtraction on 2 datetime objects to get the difference
>>> import time
>>> import datetime
>>>
>>> earlier = datetime.datetime.now()
>>> time.sleep(10)
>>> now = datetime.datetime.now()
>>>
>>> diff = now - earlier
>>> diff.seconds
10
convert your strings to datetime objects with time.strptime
datetime.strptime("2015-08-10 19:33:27.653", "%Y-%m-%d %H:%M:%S.%f")
timedelta.seconds does not represent the total number of seconds in the timedelta, but the total number of seconds modulus 60.
Call the function timedelta.total_seconds() instead of accessing the timedelta.seconds property.
For python 3.4, first you'd need to convert the strings representing times into datetime objects, then the datetime module has helpful tools work with dates and times.
from datetime import datetime
def to_datetime_object(date_string, date_format):
s = datetime.strptime(date_string, date_format)
return s
time_1 = '2015-08-10 19:33:27'
time_2 = '2015-08-10 19:31:28'
date_format = "%Y-%m-%d %H:%M:%S"
time_1_datetime_object = to_datetime_object(time_1, date_format)
time_2_datetime_object = to_datetime_object(time_2, date_format)
diff_time = time_1_datetime_object - time_2_datetime_object
I need to calculate difference between time (and if it exceed 24 hours then days)
Like:
from datetime import datetime
from time import strftime
s1 = '24:11:2014:14:28:42'
s2 = datetime.now().strftime("%d:%m:%Y:%H:%M:%S")
FMT = '%d:%m:%Y:%H:%M:%S'
timedelta = datetime.now.strftime(s2,FMT) - datetime.now.strftime(s1,FMT)
print (timedelta)
But this is not detecting more than 24 hours, If found this code which can detect the days:
from datetime import datetime
date_format = "%d/%m/%Y %H%M%S"
a = datetime.strptime('22/10/2014 090000', date_format)
b = datetime.strptime('25/11/2014 100000', date_format)
delta = b - a
print (delta.days)
What I want is something like this in return: "2 days 03:35:00 HH:MM:SS" in return"
The timedelta you are getting from b - a already has all the information you need, have a look at https://docs.python.org/2/library/datetime.html#datetime.timedelta.