简体   繁体   中英

how to convert time stamp 07 Mar 2016 01:00:03 PM to numeric in r

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM