Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two time of same date "2015/09/12 00:02:18" and "2015/09/12 23:59:39" .

How to calculate difference between these two time frames?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
185 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can try to convert the strings into POSIXct and simply calculate the difference:

t1 <- "2015/09/12 00:02:18"
t2 <- "2015/09/12 23:59:39"
> as.POSIXct(t2) - as.POSIXct(t1)
#Time difference of 23.95583 hours

Alternatively you may use difftime(), a function used to calculate time differences:

> difftime(t2, t1)
#Time difference of 23.95583 hours

If you don't want to have the text in the output, you can obtain the time difference in hours with

> unclass(difftime(t2, t1, units="hours"))[1]
#[1] 23.95583

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...