Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4320684
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:39:38+00:00 2026-05-21T08:39:38+00:00

I want to find time difference between two date and then compare the difference

  • 0

I want to find time difference between two date and then compare the difference time in hours. Something like this,

StartTime = 2011-03-10 15:45:48
EndTime = 2011-03-10 18:04:00

And then find the difference as,
timeDifference = abs(StartTime – EndTime)
And then I need to compare the results as,
If timeDifference > 6 hours
…

When I use this method, the results I got was is in time format, how should I change time format into hours in python?

Thank you,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T08:39:39+00:00Added an answer on May 21, 2026 at 8:39 am

    Let’s assume that you have your two dates and times as datetime objects (see datetime.datetime):

    >>> import datetime
    >>> start_time = datetime.datetime(2011,3,10,15,45,48)
    >>> end_time = datetime.datetime(2011,3,10,18,4,0)
    

    Subtracting one datetime from another gives you a timedelta object, and you can use abs to ensure the time difference is positive:

    >>> start_time - end_time
    datetime.timedelta(-1, 78108)
    >>> abs(start_time - end_time)
    datetime.timedelta(0, 8292)
    

    Now, to convert the seconds difference from a timedelta into hours, just divide by 3600:

    >>> hours_difference = abs(start_time - end_time).total_seconds() / 3600.0
    >>> hours_difference
    2.3033333333333332
    

    Note that the total_seconds() method was introduced in Python 2.7, so if you want that on an earlier version, you’ll need to calculate it yourself from .days and .seconds as in this answer

    Update: Jochen Ritzel points out in a comment below that if it’s just the comparison with a number of hours that you’re interested in, rather that the raw value, you can do that more easily with:

    abs(start_time - end_time) > timedelta(hours=6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find difference between two time-components, but I am not getting what
I want to find out the difference between two Dates (in terms of minutes)
Hallo, I want to find the difference between today and another date, convert todays
How can I find the difference between this, for example, 1700 hours and 1020
Let's say I want to find out the difference in seconds between two times.
I want to find a sql command or something that can do this where
I have two NSDate objects and I want the difference between the two and
I want to find records on a combination of created_on >= some date AND
so I have these two time stamps in PHP 1253339331 1253338959 I want to
I am aware that Dijkstra's algorithm can find the minimum distance between two nodes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.