In R, use lubridate to convert hms objects into seconds

tomw picture tomw · Jul 2, 2012 · Viewed 13.1k times · Source

simple question in lubridate--I want to convert an hms object into its appropriate number of seconds since the start of the day.

For instance

library(lubridate)
hms("12:34:45")

then I want to know exactly how long 12 hours, 34 minutes, and 45 seconds is, in seconds

something obvious like

seconds(hms("12:34:45"))

just returns

45s

which is not what I want. How do I convert these hms values into seconds? I'd like to use lubridate

Answer

Steven Varga picture Steven Varga · Dec 9, 2012
R>lubridate::period_to_seconds(hms("01:00:00")) 

gives expected 3600 seconds as numeric counting from 00:00:00 or in the case above:

R>period_to_seconds(hms("12:34:45"))