Difference between two dates in Julia

I am trying to get difference between two dates in number of “days”.

I am able to substract the two date fields but results is being produced as “n days”.

As we can see below DATE_DIFF is produced after executing below logic. I would like to produce DATE_DIFF as Integer like 0, 510, 1223 etc.

df2.DATE_DIFF = (df2.dte2 - minimum(df2.dte2))


3×4 DataFrame
 Row │ dte         dte2        year   DATE_DIFF 
     │     Date      Date        Int64  Day       
─────┼──────────────────────────────────────────
   1 │ 2022-01-01  2022-01-01   2022  0 days
   2 │ 2023-05-26  2023-05-26   2023  510 days
   3 │ 2025-05-08  2025-05-08   2025  1223 days

Divide by Day(1).

You can also use Dates.value but I think that’s worse because if your input units change and you forget to update you’ll get the wrong result.

For extreme values of days, dividing might lose some precision in the conversion to float.

julia> BigInt(Day(typemax(Int))/Day(1)) - BigInt(typemax(Int))
1

In that case maybe it’s better to use value(d). A best-of-both-worlds solution would be

@jar1 Thank You!! It returned float and I have converted it to Int.

@jar1 Do you know if we have any method in julia to convert hh:mm to minutes. ex. 13:06 into minutes?

What result do you want from 13:06?

60 * 13 = 780 + 06 = 786 minutes, I know we can calculate 60 * 13 by parsing or splitting by “:” but just wanted to check if there is method that already does this.

One way is:

hour(Time("13:06")) * 60 + minute(Time("13:06"))

Not sure if there is a more elegant function

1 Like

@dnb Yeah, that also works - Thanks!

julia> convert(Minute, Dates.CompoundPeriod(Time("13:06")))
786 minutes