Anne
Anne

Reputation: 387

R Change date YY-MM-DD hh:mm to time since origin (Days.Seconds)

I have a date input like this (currently as character class):

input=c("2013-05-08 11:20:10", "2013-05-08 11:21:09")

And want to have an output like this:

output=c(127.472338, 127.473032)

Which is the time since origin (2013-01-01 00:00:00) in days and seconds.

Previously, I used data in the output format and reconverted it to the input format using:

temp=as.POSIXlt(output*24*3600,origin='2013-01-01 00:00:00', tz="Etc/GMT+0")

How can I rewrite this so that I get the desired output? Thanks in advance.

Upvotes: 2

Views: 493

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176728

Use difftime (or -):

> difftime(as.POSIXct(input), as.POSIXct("2013-01-01"))
Time differences in days
[1] 127.4307 127.4314
> as.POSIXct(input) - as.POSIXct("2013-01-01")
Time differences in days
[1] 127.4307 127.4314

Upvotes: 4

Related Questions