How to calculate difference between two timestamps in number of days ?

Gaurav Kumar15
Giga Guru

Hi All,

We have a requirement to calculate the number of days between 2 datetimestamps.

e.g :-

Scenario :-

Date1 :- 05-07-2017 13:00:00

Date2 :- 06-07-2017 11:00:00

Now i try to convert both the date in secs and then took the difference and then divided by number of seconds in a day. It will give me value as slightly less than 1. If i use Math.Ceil(0.9) =1

So i will end up getting number of days as 1, but in actual it should be 2, as it used both the days.

If i use Math.Ceil(0.9) + 1 = 2 , then it will give me correct result. But i can't use this as it will lead to wrong result in below situation :-

Date1 :- 05-07-2017 13:00:00

Date2 :- 06-07-2017 14:00:00

diff will come out to be slightly higher than 1, assume it to be 1.1

here , Math.Ceil(1.1) + 1 = 2+1 = 3, which is wrong in this also the actual anwer should be 2.

So could someone please help me in understanding on how can we calculate this.

Thanks,

Gurav

4 REPLIES 4

Shishir Srivast
Mega Sage

How about using round() function?


samiul1
Tera Expert

Paramahanns
Tera Expert

Hi Gaurav,



Are you looking this kind of a result and solution.



var gdt1 = new GlideDateTime(current.arrival.getDisplayValue());


var gdt2 = new GlideDateTime(current.departure.getDisplayValue());


var dur = GlideDateTime.subtract(gdt1, gdt2);


current.duration=dur.getDisplayValue();




I wrote this in the business rules and the example result I got was as below for the input I gave in.



Start Date: 2017-07-26 00:37:24


End Date: 2017-07-27 01:37:30


Duration Difference: 1 Day 1 Hour



Please mark as helpful or Correct based on the impact.



Regards


Param