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 8446505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:57:11+00:00 2026-06-10T09:57:11+00:00

I’ve spent the past hour digging around the Python docs and many SO questions;

  • 0

I’ve spent the past hour digging around the Python docs and many SO questions; please forgive me for being another Python newbie trapped by the mystery of time difference in Python.

My goal is to determine the difference between the current time and a certain date/time regardless of being in the past/future and return a workable format such as seconds.

For example, if the inputs are 2:00PM and 4:00PM (now), I’d like it to say “-7200”, representing the event occurred two hours AGO. If the inputs are Fri 4:00PM (now) and Sun 5:00PM the output should be “176400” seconds, representing two days and 1 hour from now.

Here are the things I’ve tried…

  • My first version was a function that took a string timestamp and pieced it to multiple variables, and then compared them. It was clunky with many errors and I imagine if I posted it here I would be responsible for a programmers throwing up.

  • I stumbled upon this magical timedelta function and explored the docs and SO, but I don’t think it does what I’m looking for.

  • I had the idea to convert both timestamps into seconds since epoch and then subtract, but this becomes a problem if the subtraction is in the wrong order (different cases if the event is in the future), and I feel like adding if statements to check the sign of the seconds would be clunky and something to avoid.

This is my current code (still needs to be fixed for ‘bi-directional’ comparison), from a previously resolved SO question:

now = time.strftime("%a %b %d %H:%M:%S %Y")
then = time.ctime(os.path.getmtime("x.cache"))
tdelta = datetime.strptime(now, '%a %b %d %H:%M:%S %Y') - datetime.strptime(then, '%a %b %d %H:%M:%S %Y')

And I feel like I should somehow be able to pull seconds from this, like in this question:
Python's timedelta: can't I just get in whatever time unit I want the value of the entire difference?

But I am at a lost on how to connect these dots.

How can I accomplish this?

  • 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-10T09:57:12+00:00Added an answer on June 10, 2026 at 9:57 am

    You should be able to use

    tdelta.total_seconds()
    

    to get the value you are looking for. This is because tdelta is a timedelta object, as is any difference between datetime objects.

    A couple of notes:

    1. Using strftime followed by strptime is superfluous. You should be able to get the current datetime with datetime.now.
    2. Similarly, using time.ctime followed by strptime is more work than needed. You should be able to get the other datetime object with datetime.fromtimestamp.

    So, your final code could be

    now = datetime.now()
    then = datetime.fromtimestamp(os.path.getmtime("x.cache"))
    tdelta = now - then
    seconds = tdelta.total_seconds()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

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.