Adding days to a Date

I was trying to calculate the difference between 2 dates. I was using code that I found on a thread that used Clock1.Duration and Clock1.makeDate for the Start and End of the duration.
The problem was I was getting the wrong results where the dates were on different sides of the Daylight savings crossover dates.
26-March-2022 to 29-March-2022 was giving me a result of 2 days instead of 3. It seemed to be generating the two dates (one with dst the other not) and then calculating the difference.

So instead I am using a rounding of milliseconds to round the days off, which works for my purpose.

Anyone got any light to shine on this topic, an more correct solution

To be precise, 2 days and 23 hours. Everything explained correctly.

Just a rough solution, but you could do something like this:

You could throw in a similar solution for October as well


I prefer my solution to round the millisecond difference between "start" to "end" dates. This works fine from my testing and doesn't require conditionals. (BTW: your condition doesn't work for april etc..., it still floors the result until the end of Daylight savings). (if the end-date was 30th-june-2022 still requires +1 as it is in DST and start-date isn't)

The problem with the DurationToDays function is a rounding vs flooring. The DurationToDays function seems to Floor the result (2.958333 days is reported as 2), where a Rounded result is probably what people will expect.

You are correct, it was only a rough solution that didn't consider a wider date span :slight_smile:
A more considered approach may be more accurate than rounding

Why not use conditionals ? They are very handy :wink:

IF both dates are in the same year, you can use Day of year

image

This is a nice solution, I might change the "=23" to be >=22, just in case of leap seconds or weird instances.
Super stuff.
Thanks Anke.

1 Like

sadly, the use case is for dates in different years.
but thanks for that.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.