0

I need to get difference between 2 days, not big deal. But when i go from eg: diff days between 2019-06-27 11:00 and 2019-06-30 08:00 (3 days diff) it return 4 days. Every difference > 2 is jumping the 3rd day and returning 1 extra day.

I'm working with Ubuntu 18.04 LTS and tried on current last version of Google Chrome Versión 75.0.3770.100 (Build oficial) (64 bits) I tried momentjs with same problem, then moved to Date object native of javascript and the same problem.

var oneDay=24*60*60*1000;
var firstDay= new Date(pickUpDate[2],pickUpDate[1]-1,pickUpDate[0],0,0,0);
var secondDay= new Date(returnDate[2],returnDate[1]-1,returnDate[0],0,0,0);
days=(secondDay.getTime()-firstDay.getTime())/oneDay;
console.log(days)

//input:pickUpDate[]=["01","07","2019"]
//returnDate[]=["03","07","2019"]
//output: 2
//input:pickUpDate[]=["01","07","2019"]
//returnDate[]=["04","07","2019"]
//output: 4


Date1= moment(pickUpDate+' '+phour,'DD/MM/YYYY HH:mm');
Date2=moment(returnDate+' '+rhour,'DD/MM/YYYY HH:mm');
days=moment.duration(Date2.diff(Date1))/(1000*60*60*24);
console.log(days)

//input:pickUpDate="01/07/2019"
//returnDate="03/07/2019"
//phour="11:00"
//rhour="08:00"
//output: 2.rightdecimals
//input:pickUpDate="01/07/2019"
//returnDate="04/07/2019"
//phour="11:00"
//rhour="08:00"
//output: 4.rightdecimals

//Also tried moment.duration().asDays() and the same history.

No errors on the console

EDIT

Tried same code at Windows 10 and latest Chorme and is working perfectly, hope somebody have some idea what's going on. My day time is (UTC-06:00) Guadalajara, Ciudad de México, Monterrey

  • 1
    Are you *sure* these are the inputs you are passing? I ran your exact code examples and got `3` when using the `Date` object and `2.875` when using Moment (both with the second set of inputs). – Matt Johnson-Pint Jun 28 '19 at 17:32
  • 1
    Not your problem, but days are not all 24 hours long where daylight saving is observed, see [*How do I get the number of days between two dates in JavaScript?*](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – RobG Jun 28 '19 at 20:32
  • So, how can i avoid this daylight saving problems. I'm at México, tried same code on windows and it's working perfectly. Any idea? – Amir Alcocer May Jun 29 '19 at 23:00

0 Answers0