David Maij
David Maij

Reputation: 53

Weird strptime outcome for date-time substraction

I want to subtract a list of time-dates from another in R, to calculate the time in between in minutes. Say I want to subtract 5-7-2017-11:33:31:543 from 5-7-2017-11:36:8:241. As I understand strptime(), I should do the following:

strptime("5-7-2017-11:36:8:241","%d-%m-%Y-%H:%M:%OS") - strptime("5-7-2017-11:33:31:543","%d-%m-%Y-%H:%M:%OS")
op<-options(digits.secs=3)

This gives me the following answer: Time difference of 2.616667 mins

While I am actually looking for 2.37 something.

I can do .616667 * 60, which will give me .37, but I'd rather not for the entire list of data that I have. Any help or explanation is very much appreciated, thanks in advance for your time!

Upvotes: 0

Views: 37

Answers (1)

Oriol Mirosa
Oriol Mirosa

Reputation: 2826

You could use lubridate's as.period:

library(lubridate)

as.period(strptime("5-7-2017-11:36:8:241","%d-%m-%Y-%H:%M:%OS") - strptime("5-7-2017-11:33:31:543","%d-%m-%Y-%H:%M:%OS"))

## "2M 37S"

Upvotes: 2

Related Questions