2

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.

Anne
  • 377
  • 2
  • 4
  • 16

1 Answers1

4

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
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418