Reputation: 659
I have two timestamps
a<-"2016-11-24 08:30:00"
b<-"31Jul2016 21:26:00"
I want to compare which timestamp is earlier. How can I do that? How can I make sure the timestamps are in the same format so that they are comparable?
Upvotes: 0
Views: 197
Reputation: 388817
We can use anytime
library to compare date-time in different formats. It converts them into "POSIXct" "POSIXt"
class so that it is easy to compare them.
library(anytime)
anytime(a) > anytime(b)
#[1] TRUE
where,
anytime(a)
#[1] "2016-11-24 08:30:00 IST"
anytime(b)
#[1] "2016-07-31 21:26:00 IST"
Upvotes: 1
Reputation: 1247
Use strptime
to covert you character to date:
a <- "2016-11-24 08:30:00"
b <- "31Jul2016 21:26:00"
aa <- strptime(a, "%Y-%m-%d %H:%M:%S")
bb <- strptime(b, "%d%b%Y %H:%M:%S")
Then you can check which timestamp is earlier.
> aa < xx
[1] FALSE
> aa > xx
[1] TRUE
Upvotes: 1