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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:41:38+00:00 2026-06-10T00:41:38+00:00

Ive been searching quite a lot find an answer for this , and couldn’t

  • 0

Ive been searching quite a lot find an answer for this , and couldn’t find one.

I have 2 dates ,
I want to calc days between them

but I Also want the end day to be counted.

Example :

  • me and my wife go to hotel from [20 jan] till [26 Jan] , so its 7 days total.

the only code which I find working is :

 Math.round((b - a) / ( 1000 * 60 * 60 * 24)) +1 

where :

var a= new Date (y,0,20);
var b= new Date (y,0,26);

I also made a nested loop to test all months within a 150 years , and it works Ok.

var y = 1970;
var m = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
for(var i = 0; i < 150; i++)
{
    for(var j = 0; j < 12; j++)
    {
        var a = new Date(y + i, m[j], 20);
        var b = new Date(y + i, m[j], 26);
        var days = Math.round((b - a) / (1000 * 60 * 60 * 24)) + 1;
        if(days != 7) console.log(i + y); //tell me if result is unexpected.
    }
}
console.log('-FINISH-' + new Date().getTime())

So where is the problem ?

I can’t figure how the math function like round can do the trick here.

We are talking about milliseconds here and I can’t see where the round behavior gives me the right result.

(p.s. – forget about ceil and floor , they do not help here and have inconsistent results) , I have also tried to add one day to the b value , and use ceil || floor but with no success

here is the code

  • 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-10T00:41:39+00:00Added an answer on June 10, 2026 at 12:41 am

    The reason your rounding is working is because you’re only working with full days. myDate.getTime() will yield the number of milliseconds since 1970-01-01. If you’re always assigning your dates as new Date(y,m,d) you will always have the time part set to 00:00:00.000, and hence the date comparison will always yield a multiple of 86400000, which is your divisor. The rounding here is for the most part superfluous.

    If you’re creating all of your dates as specified above, the only time rounding does come into play, is when the daylight savings offset at date b is different from that at date a. round will take care of these discrepancies, as they’re rarely more than an hour.

    From your script, October 1970 is problematic (in CEST) because Oct 20th is in daylight savings, and Oct 26th isn’t.

    var a = new Date(1970, 9, 20);
    var b = new Date(1970, 9, 26);
    
    (b - a) / (1000 * 60 * 60 * 24) // 6.041666666666667
    

    You could work around this by rounding, or by using UTC dates

    var a = new Date(Date.UTC(1970, 9, 20));
    var b = new Date(Date.UTC(1970, 9, 26));
    
    (b - a) / (1000 * 60 * 60 * 24) // 6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been searching for an answer for this and cant find one, although I
I've been searching quite a bit now but cannot find the answer to this.
I've been searching around the web for an answer to this one, but was
I've been searching for this for quite a while but can't seem to find
This been bugging me for quite a few hours. I've been searching a lot
I've been searching for answers for quite some time on this as it continues
I've done quite a bit of searching, but haven't been able to find exactly
I am getting this in my error log file. Ive been searching for asolution
I've been searching around but could not find any answers to this. What I'm
I've been searching around but can't find much about this, but would cross-casting from

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.