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

  • SEARCH
  • Home
  • 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 7770665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:27:51+00:00 2026-06-01T16:27:51+00:00

i am subtracting two dates and computing the difference in hours c_date = 2011-03-23

  • 0

i am subtracting two dates and computing the difference in hours

c_date = "2011-03-23 12:52:14"
c_obj_date = datetime.datetime.strptime(c_date, "%Y-%m-%d %H:%M:%S")
n_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print "Creation Date:", c_date
print "Current Date: ", n_date
hours = abs(n_date - c_obj_date).total_seconds() / 3600.0
print hours

however this gives me this error:

  File "./eight.py", line 69, in zabbix_result
    hours = abs(n_date - c_obj_date).total_seconds() / 3600.0
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.datetime'

i am using python2.4

UPDATE:

i now used

n_date = datetime.datetime.now()

but the total_seconds() wont work in python2.4?? any idea how i can subtract two datetime object and get the hour difference?

  • 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-06-01T16:27:52+00:00Added an answer on June 1, 2026 at 4:27 pm

    n_date is a string and c_obj_date is a datetime.datetime object. I’m not sure where you’re getting the .total_seconds() method from either (it’s not a timedelta member…). Try something like this:

     c_date = "2011-03-23 12:52:14"
     c_obj_date = datetime.datetime.strptime(c_date, "%Y-%m-%d %H:%M:%S")
     n_date = datetime.datetime.now()
    
     print "Creation Date:", c_date
     print "Current Date: ", n_date
    
     absdiff = abs(n_date - c_obj_date)
     hours = (absdiff.days * 24 * 60 * 60 + absdiff.seconds) / 3600.0
    
     print hours
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Ruby stdlib have any objects that represent difference between two timestamps? Subtracting two
Possible Duplicate: Subtracting dates in PHP I have two Unix timestamps, how can I
How would i write an expression that gives me the difference between two dates
I've got a little problem by subtracting two datetime objects from each other. I
This question is similar to this question about subtracting dates with Python , but
I have two unix timestamps in my database that I am subtracting to get
I try to subtract two dates from each other but unfortunately without success. Maybe
I have a problem in subtracting a two values. i just want to be
Using php I'd like to subtract/minus two times; each time within a separate document
I have two lists in python and I want to know if they intersect

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.