sclee1
sclee1

Reputation: 1281

Why not show the hour,time and second when I use as.Date in R

I coded the below in R and I want to see the hour,time and second format. However, when I ran the code, it just shows the year,month and day even though I specified the format correctly.

> val <- 12016539307200
> valD <- as.Date(as.POSIXct(val, origin="1970-01-01"),format="%Y%m%d %H%M%S")
> valD
[1] "382758-12-22"

Could you give me a way to solve this issue?

Upvotes: 1

Views: 68

Answers (2)

Patrik_P
Patrik_P

Reputation: 3200

If it contains milliseconds, you go for the following:

as.POSIXct(val/1000, origin="1970-01-01")
"2350-10-16 09:35:07 CEST"

or

library(anytime)
anytime(12016539307200/1000)
"2350-10-16 09:35:07 CEST"

Upvotes: 0

jlesuffleur
jlesuffleur

Reputation: 1253

Because it is a Date object, representing a calendar date. To have an object representing time, keep it in POSIXct:

> val <- 12016539307200
> valD <- as.POSIXct(val, origin="1970-01-01", tz = "UTC")
> valD
[1] "382758-12-22 01:20:00 UTC"

Upvotes: 3

Related Questions