-2

Im trying to find time difference between two timestamps, say, start and endtime. But the stamps are in string format :07 Mar 2016 01:00:03 PM. Can someone help how to convert this in numeric and find the time difference? Thanks

  • What did you try? Why did it not work? Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Heroka Mar 19 '16 at 11:36

1 Answers1

5

lubridate is your new best friend so download it right away to get to know her.

install.packages('lubridate')
library(lubridate)

Based on your example, use dmy_hms()

a <- dmy_hms('07 Mar 2016 01:00:03 PM')
# "2016-03-07 13:00:03 UTC"
b <- dmy_hms('08 Mar 2016 01:00:03 PM')
# "2016-03-08 13:00:03 UTC"

Then a simple subtraction could get the date diff.

b - a
# Time difference of 1 days

Or you can also use the as.numeric() use this number for calculation later.

as.numeric(b - a)
# 1
halfer
  • 19,824
  • 17
  • 99
  • 186
BGA
  • 563
  • 6
  • 14