Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between dates with HH:MM:SS format

I have a DF and I want to make the difference between dates (date and time) with HH:MM:SS result. See my example:

DATE_1 <- c('06-05-2022 11:06:48','04-05-2022 06:11:55','17-02-2022 21:06:50')
DATE_2 <- c('06-05-2022 12:15:00','04-05-2022 06:35:00','18-02-2022 21:21:00')
DF <- data.frame(DATE_1 = lubridate::dmy_hms(DATE_1), DATE_2 = lubridate::dmy_hms(DATE_2))

My intended result is:

DF <- DF %>% 
  mutate(DIF = c('01:08:12','00:23:05','24:14:10'))

I don’t want my result to come in the format like this: 1H 31M 10S or 150d 21H 14M 10S.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Another solution with hms::as_hms:

DF %>% mutate(DIF = hms::as_hms(difftime(DATE_2,DATE_1)))

               DATE_1              DATE_2      DIF
1 2022-05-06 11:06:48 2022-05-06 12:15:00 01:08:12
2 2022-05-04 06:11:55 2022-05-04 06:35:00 00:23:05
3 2022-02-17 21:06:50 2022-02-18 21:21:00 24:14:10
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading