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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:45:52+00:00 2026-06-19T01:45:52+00:00

I want to find difference between two Dates. For that I did subtract one

  • 0

I want to find difference between two Dates. For that I did subtract one Date object from another Date object. My code is as follows :

var d1 = new Date(); //"now"
var d2 = new Date(2012,3,17); // before one year
document.write("</br>Currrent date : "+d1);
document.write("</br>Other Date : "+d2);
document.write("</br>Difference : "+new Date(Math.abs(d1-d2)));

But the result is not as I expected:

Currrent date : Sun Feb 17 2013 02:58:16 GMT-0500 (EST)
Other Date : Sat Jan 21 2012 00:00:00 GMT-0500 (EST)
Difference : Thu Jan 28 1971 21:58:16 GMT-0500 (EST)

I want to calculate the (1 year) difference between them.

Thanks

  • 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-19T01:45:53+00:00Added an answer on June 19, 2026 at 1:45 am

    So fundamentally the biggest exact date unit is a week which accounts for 7 * 86400 seconds. Months and Years are not exaclty defined. So assuming you want to say “1 Month ago” if the two dates are e.g. 5.1.2013 and 5.2.2013 or 5.2.2013 and 5.3.2013. And saying “1 Month and 1 day ago” if you have e.g. 5.1.2013 and 6.2.2013, then you would have to use a calculation like this:

    // dateFrom and dateTo have to be "Date" instances, and to has to be later/bigger than from.
    function dateDiff(dateFrom, dateTo) {
      var from = {
        d: dateFrom.getDate(),
        m: dateFrom.getMonth() + 1,
        y: dateFrom.getFullYear()
      };
    
      var to = {
        d: dateTo.getDate(),
        m: dateTo.getMonth() + 1,
        y: dateTo.getFullYear()
      };
    
      var daysFebruary = to.y % 4 != 0 || (to.y % 100 == 0 && to.y % 400 != 0)? 28 : 29;
      var daysInMonths = [0, 31, daysFebruary, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    
      if (to.d < from.d) {
        to.d   += daysInMonths[parseInt(to.m)];
        from.m += 1;
      }
      if (to.m < from.m) {
        to.m   += 12;
        from.y += 1;
      }
    
      return {
        days:   to.d - from.d,
        months: to.m - from.m,
        years:  to.y - from.y
      };
    }
    // Difference from 1 June 2016 to now
    console.log(dateDiff(new Date(2016,5,1), new Date()));

    As I said, it gets tricky 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find out the difference between two Dates (in terms of minutes)
I want to find time difference between two date and then compare the difference
i want to find find differences between two dates, where m giving date value
We want to find the number of days between two dates. This is simple
Help me please to find out what's the difference between these two code snippets:
I want to find difference between two time-components, but I am not getting what
I want to find the difference between two numbers in a range, but I
Let's say I want to find out the difference in seconds between two times.
Is there any way find difference between two cs files. I already ask question
I want to know what the difference between these two foreignkey definitions are. (1)

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.