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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:17:36+00:00 2026-06-17T04:17:36+00:00

Using Date.js already, but can also use another library if necessary. Not sure what

  • 0

Using Date.js already, but can also use another library if necessary.

Not sure what is the best way to work with time deltas. Specifically, I want to display the time that has elapsed between now and a past date-time.

So I need to do something like this:

var elapsed_time = new Date() - pastDate;
pastDate.toString('days-hours-minutes-seconds');

Gotten it to mostly work using Date.js, but the problem is now I’m working with a Date object and not a timespan, so what should be an 23 hour time span is instead 23 hours after the Date’s very first time:

var result = (new Date()) - past_date;
"result" is the number (probably milliseconds): 15452732
var result = (new Date() - past_date
"result" is a date from 1969: Wed Dec 31 1969 23:17:32

What I need is:

0 days 23 hours 17 minutes and 32 seconds

Any ideas?

  • 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-17T04:17:38+00:00Added an answer on June 17, 2026 at 4:17 am

    Sounds like you need moment.js

    e.g.

    moment().subtract('days', 6).calendar();
    

    => last Sunday at 8:23 PM

    moment().startOf('hour').fromNow();
    

    => 26 minutes ago

    Edit:

    Pure JS date diff calculation:

    var date1 = new Date("7/Nov/2012 20:30:00");
    var date2 = new Date("20/Nov/2012 19:15:00");
    
    var diff = date2.getTime() - date1.getTime();
    
    var days = Math.floor(diff / (1000 * 60 * 60 * 24));
    diff -=  days * (1000 * 60 * 60 * 24);
    
    var hours = Math.floor(diff / (1000 * 60 * 60));
    diff -= hours * (1000 * 60 * 60);
    
    var mins = Math.floor(diff / (1000 * 60));
    diff -= mins * (1000 * 60);
    
    var seconds = Math.floor(diff / (1000));
    diff -= seconds * (1000);
    
    document.write(days + " days, " + hours + " hours, " + mins + " minutes, " + seconds + " seconds");
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already attempted using py2exe (not compatible with ipy) and PYC (out of date).
I already asked a similar question but still can't get it to work. I
I tried using date/time, time, but it doesn't help. Exception Details: System.Web.HttpException: DataBinding: 'DatePickerControl.DatePicker'
I have tried using date(m/d/Y, strtotime(04-05-2012)) but I will get 05/04/2012 or on some
I'm using AWS' SimpleDB as intended for the most part, but also want to
Hi I am using date() in my script to get todays date automatically. On
Is there any reason behind using date(January 1st, 1970) as default standard for time
I have dates stored in MongoDB using Date(), MongoDB uses UTC it's date type,
I am trying to output dates in the Italian format using date() as follows:
i have one problem when i sort the NSMutablearray using date and time wise.

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.