Join us on July 16th as we explore real-world Reinforcement Learning Learn more

Difference in minutes and seconds between two date

Level 1
Difference in minutes and seconds between two date
Hi everyone,

How can I get the difference in "hours" "minutes" AND "seconds" (example: 14:30:24) from two date?



Example:

Col 1: 2018-01-01T09:50:15.000Z

Col 2: 2018-01-01T10:07:55.000Z

Col 3 (difference): 00:17:40

Thank so much for your help

Maxime
0 Kudos
3 Replies
Dataiker
Dataiker
Hello Maxime,

You can for example use a Prepare recipe to compute the time difference between two columns (these dates have to be parsed). For this, you can use the processor "Compute difference between two dates" in the Prepare recipe and select the two columns.

You can define the granularity of the output right in the new step (from years to seconds).



I hope this helps!

Have a nice day.

Pauline
0 Kudos
Level 1
Author
Hello,
Thank you for your answer but with the processor "Compute difference between two dates" the output time unit is hours or minutes or seconds but cannot be hours:minutes:seconds.
0 Kudos
Dataiker
Dataiker
Indeed! Thanks for replying. I tried this on my side and it seems to be working stillusing the formula in a Prepare recipe.
The idea is to rebuild the date with hours:minutes:secondes, from the time difference in seconds.

"00"[0,2-(floor(diff(date1, date2, "seconds")/3600)).length()] + (floor(diff(date1, date2, "seconds")/3600))
+ ':'
+ "00"[0,2- (floor(mod(diff(date1, date2, "seconds"), 3600)/60)).length()] + (floor(mod(diff(date1, date2, "seconds"), 3600)/60))
+ ':'
+ "00"[0,2- (mod(mod(diff(date1, date2, "seconds"), 3600),60)).length()] + (mod(mod(diff(date1, date2, "seconds"), 3600),60))

You can obviously use a Python recipe instead of the formula to rebuild this field too.

I hope this helps.
Have a nice day,
Pauline
0 Kudos
Labels (2)