0

I have to calculate the difference between two selected dates. The day start is considered 12am and the day end is the next 12am.

Javascript code:

const date1 = new Date('2019-06-12T10:30:00Z');
const date2 = new Date('2019-06-14T10:30:00Z');
const diffTime = Math.abs(date2.getTime() - date1.getTime());
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(diffDays);

I get the difference as 2 days which is not what I want, as day is calculated from 12am. The actual answer should be 3 days. What needs to be changed in the code to get the output 3 days? i.e.

1) First Day  : 12 June 10:30am - 13 June 12:00am 
2) Second Day : 13 June 12:00am - 14 June 12:00am 
3) Third Day  : 14 June 12:00am - 14 June 10.30am 

Any help would be appreciated.. Thanks!

David Callanan
  • 5,601
  • 7
  • 63
  • 105
Supritha
  • 79
  • 2
  • 12
  • You can't say that the beginning and the end of the day are at the same time. If you declare that the beginning of the day is at 12 am, the end will be at 11:59.999. The difference between new Date('2019-06-12T10:30:00Z') and new Date('2019-06-14T10:30:00Z') are, of course, 2 exact days. – virgiliogm Jun 07 '19 at 10:56
  • From my logic, there are 2 days, 48 hours ;-? how did you get 3 days? – Mara Black Jun 07 '19 at 10:56
  • diffTime value you have written which return the days encountered by the time duration between the two dates. So that, the exact difference given by you is 2 days in same time of two dates.If you want to get total days encountered between two days the time should be different. ex : date1 = new Date('2019-06-12T10: 20 :00Z'); (in your variable) – Sakthikanth Jun 07 '19 at 10:59
  • 3 days i.e. 1) First Day: 12 june 12:am - 13 june 12:am 2) Second Day: 13 june 12:am to 14 june 12:am 3) Third Day : 14 june 12:am to 14 june 10.30am – Supritha Jun 07 '19 at 11:09
  • you can get 3 encountered days between jun 12th 12am and jun 14th 10:30.am . from and to date's time should be different – Sakthikanth Jun 07 '19 at 11:19
  • You say the third day is `14 june 12am to 14 june 10.30am`. That time is going backwards, did you mean `10.30pm`? If you fix this, I think it might solve the problem. – David Callanan Jun 07 '19 at 11:22
  • No, june 14th 10.30 am – Sakthikanth Jun 07 '19 at 11:23
  • Ah, I made a mistake. I thought `12:00am` was in the afternoon, but it's actually midnight. – David Callanan Jun 07 '19 at 11:26
  • @Sakthikanth . "from and to date's time should be different" . i didnt understand. can u explain. thanks – Supritha Jun 07 '19 at 11:44
  • 1
    Sorry for repeating my comment: You can't say that the beginning and the end of the day are at the same time. If the beginning of the day is at 12 am, the end will be at 11:59.999pm. The difference between new Date('2019-06-12T10:30:00Z') and new Date('2019-06-14T10:30:00Z') are, of course, 2 exact days – virgiliogm Jun 07 '19 at 11:48
  • 1
    @Supritha The start time (date1's time) and end time (date2's time) of the two dates different should be greater than the 48 hours to get the total encountered days between the days. – Sakthikanth Jun 07 '19 at 11:50
  • 1
    date1 = new Date('2019-06-12T00:00:00Z'); date2 = new Date('2019-06-14T10:30:00Z'); – Sakthikanth Jun 07 '19 at 11:56

3 Answers3

1

It looks like you want to count any portion of a day as a full day?

Try setting the time of the first date to 00:00:00 and the time of the second date to 23:59:59 and then your calculation is fine.

const date1 = new Date('2019-06-12T10:30:00Z');
const date2 = new Date('2019-06-14T11:30:00Z');

date1.setSeconds(0);
date1.setMinutes(0);
date1.setHours(0);
date2.setSeconds(59);
date2.setMinutes(59);
date2.setHours(23);

const diffTime = Math.abs(date2.getTime() - date1.getTime());
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
alert(`${diffTime} - ${diffDays}`);
David Brunning
  • 575
  • 7
  • 18
1

If your timestamps and "days" are always UTC, you can set the time to 00:00:00 UTC, get the difference in days and add one. In ECMAScript, UTC days are always exactly 24 hours long (they ignore seconds and daylight saving).

You can effectively set the time to 00:00:00 by simply using the built–in parser and the date part only, as due to a quirk in ECMA-262, ISO 8601 format dates are treated as UTC. However, I'd suggest writing a simple parser (2 or 3 lines) instead, e.g.

let d0 = '2019-06-12T10:30:00Z';
let d1 = '2019-06-14T11:30:00Z';

/* Get difference in days between two dates.
** @param {string} ts0 - timestamp in format YYYY-MM-DDTHH:mm:ssZ
** @param {string} ts1 - timestamp in format YYYY-MM-DDTHH:mm:ssZ
** @returns {number} difference between timestamps in days, rounded up
*/
function diffDays(ts0, ts1) {
  function qParse(s) {
    let b = s.split(/\D/);
    return new Date(Date.UTC(b[0], b[1]-1, b[2]));
  }
  return (qParse(ts1) - qParse(ts0)) / 8.64e7 + 1;
}

console.log(diffDays(d0, d1));
RobG
  • 142,382
  • 31
  • 172
  • 209
0

If the day start and day end time are both the same time, then the last day won't be counted.

What you really want is the day end time to be 12:00am on the next day (as you've stated in your question).

function countDays(dateA, dateB)
{
  // Day start time is 12:00am
  // 12:00am is represented as 0 hours
  dateA.setHours(0);
  dateA.setMinutes(0);
  dateB.setSeconds(0);

  // Day end time is 12:00am on NEXT day
  // Adding 1 to the date gives the next day
  dateB.setHours(0);
  dateA.setMinutes(0);
  dateB.setSeconds(0);
  dateB.setDate(dateB.getDate() + 1);

  // Compute difference
  const diffTime = dateB.getTime() - dateA.getTime();
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

  return diffDays;
}

JSFiddle: https://jsfiddle.net/cefv4jzp/4/

David Callanan
  • 5,601
  • 7
  • 63
  • 105