2

I have the following dataset:

start_date <- c("2020-07-01 04:00:00", "2021-01-01 12:01:01")
end_date <- c("2021-01-10 01:12:11", "2021_02-02 12:12:12")
dates <- data.frame(start_date, end_date)

I am trying to calculate the total time between the start and end date. I started by parsing the datetime using

dates <- dates %>%
  mutate(start_date = parse_datetime(start_date, "%m/%d/%Y %H:%M:%s"))


dates <- dates %>%
  mutate(end_date = parse_datetime(end_date, "%m/%d/%Y %H:%M:%s"))

I then subtracted the two using:

dates <- dates %>%
  mutate(date_diff = start_date - end_date)

But I ended up with a value rounded to the nearest hour wherein I wanted a value rounded to the nearest second. How can I do that?

  • 1
    To set the `unit` in `difftime` (and parse the date-time correctly), please see [How to make time difference in same units when subtracting POSIXct](https://stackoverflow.com/questions/30510044/how-to-make-time-difference-in-same-units-when-subtracting-posixct) – Henrik Aug 03 '21 at 17:36

1 Answers1

0

It's not a rounding issue. It's a formatting issue. And your sample code is incorrect: the format strings don't match the date strings, and one of the date strings has an _ where it should have a -.

library(tidyverse)
library(hms)

tibble() %>%
  mutate(
    start_date = parse_datetime(c("2020-07-01 04:00:00", "2021-01-01 12:01:01")),
    end_date = parse_datetime(c("2021-01-10 01:12:11", "2021-02-02 12:12:12")),
    date_diff = start_date - end_date,
    x=as_hms(date_diff)
  ) %>%  
  as_tibble()
# A tibble: 2 x 4
  start_date          end_date            date_diff       x          
  <dttm>              <dttm>              <drtn>          <time>     
1 2020-07-01 04:00:00 2021-01-10 01:12:11 -192.88346 days -4629:12:11
2 2021-01-01 12:01:01 2021-02-02 12:12:12  -32.00777 days - 768:11:11
Limey
  • 10,234
  • 2
  • 12
  • 32