Python Forum
calculating the datetime objects to get the minutes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calculating the datetime objects to get the minutes
#1
Hi all,

I am working on the datetime objects to calculate on between the start and stop time. I want to get the return output to something like this:

For 55 minutes
55
So when I try this:

self.program_start_time = [datetime.datetime(2018, 4, 3, 23, 15), datetime.datetime(2018, 4, 3, 23, 15), datetime.datetime(2018, 4, 3, 22, 45), datetime.datetime(2018, 4, 3, 23, 20), datetime.datetime(2018, 4, 3, 23, 5), datetime.datetime(2018, 4, 3, 23, 0), datetime.datetime(2018, 4, 3, 23, 0)]
self.program_stop_time = [datetime.datetime(2018, 4, 4, 0, 10), datetime.datetime(2018, 4, 4, 0, 15), datetime.datetime(2018, 4, 4, 0, 5), datetime.datetime(2018, 4, 4, 0, 20), datetime.datetime(2018, 4, 4, 0, 10), datetime.datetime(2018, 4, 4, 1, 0), datetime.datetime(2018, 4, 4, 1, 0)]

program_start_time = self.program_start_time[0])
program_stop_time = self.program_stop_time[0])
program_minutes = program_stop_time - program_start_time
program_minutes = program_minutes
I will get the return output like this:

0:55:00
I have got no idea how I can get the minutes because the type of the program_minutes is datetime.timedelta. The type for the variable program_start_time is datetime.datetime and program_stop_time is datetime.datetime.

I want to know how do you use to calculate on the two datetime objects to get the return output for the minutes instead of the hours and seconds?

Thanks in advance.
Reply
#2
Like this,and next time make some effort to post code that can be executed.
Make it easier to help.
import datetime

program_start_time = datetime.datetime(2018, 4, 3, 23, 15)
program_stop_time = datetime.datetime(2018, 4, 4, 0, 10)
program_minutes = program_stop_time - program_start_time
diff_in_minutes = program_minutes / datetime.timedelta(minutes=1)
print(diff_in_minutes)
print(f'The difference in minutes is {diff_in_minutes:.0f}')
Output:
55.0 The difference in minutes is 55
The best out there Pendulum(as it's build on datetime in bottom).
>>> import pendulum

>>> start = pendulum.create(2018, 4, 3, 23, 15)
>>> stop = pendulum.create(2018, 4, 4, 0, 10)
>>> start.diff(stop).in_minutes()
55
>>> start.diff(stop).in_seconds()
3300
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Could you help with this bug I have looked for like 20 minutes ryder5227 1 2,190 Sep-29-2019, 10:58 PM
Last Post: jefsummers
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,589 Sep-18-2019, 08:32 AM
Last Post: buran
Sad Twitch Chat Bot - Disconnects after 10 minutes sp4wny 0 3,886 Dec-20-2017, 08:28 AM
Last Post: sp4wny
  just a trial. Please wait 5 minutes before deleting this thread sylas 2 3,419 Jun-06-2017, 03:34 PM
Last Post: snippsat
  Subtract Minutes from Datetime.Time tkj80 2 43,028 May-11-2017, 09:56 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020