Reputation: 277
I'm hoping someone here knows what's going on. I'm having this strange issue where when I'm trying to convert some date/times to UNIX time I get negative numbers, here is my code for reference:
> foo <- '4/1/2017 00:02:23'
> alpha = as.POSIXct(foo)
> alpha
[1] "0004-01-20 LMT"
> beta = as.numeric(alpha)
> beta
[1] -62039347125 #Why?!?!?!
So basically, I go through the above and I end up with a negative number, the annoying thing is that no matter what date/Time I am using, I end up with the same result. The format of date/time is month/day/year hour:minute:second.
Does anyone know what's going on? Any help will be appreciated, thanks.
Upvotes: 0
Views: 775
Reputation: 277
I have figured it out. You need to specify the format of the date time..
So:
> foo <- '4/1/2017 00:02:23'
> alpha = as.POSIXct(foo, format = '%m/%d/%Y %H:%M:%S')
> beta = as.numeric(alpha)
> beta
[1] 1491001343
Upvotes: 0
Reputation: 2826
Notice that you're using the year 4, that is, 1966 years before Unix time started. You should do:
foo <- '2017/4/1 00:02:23'
alpha = as.POSIXct(foo)
alpha
## "2017-04-01 00:02:23 EDT"
beta = as.numeric(alpha)
beta
## 1491019343
Upvotes: 1