Sebastian Celis
Sebastian Celis

Reputation: 51

Date-time to numeric and backwards

Dates/Times in R I have this date: "2016-10-29 15:00:00" and i want to convert it to numeric and backwards to the same date and time i had. I used this to convert it to numeric: as.numeric(as.POSIXct("2016-10-29 15:00:00")) How i can get back my initial date and time?

    "2016-10-29 15:00:00"
    as.numeric(as.POSIXct("2016-10-29 15:00:00"))
   1477771200

I obtain that answer, but i need it back to "2016-10-29 15:00:00". What should i do?

Upvotes: 2

Views: 697

Answers (2)

SymbolixAU
SymbolixAU

Reputation: 26258

You can use as.POSIXct() on your number, but you also need to supply the origin and (probably) timezone

as.POSIXct(1477713600, origin = "1970-01-01", tz = "Australia/Melbourne")
"2016-10-29 15:00:00 AEDT"

The as.POSIX() comamnd needs to know from which reference point the numeric starts. This is usually the Unix Epoch of 1970-01-01


The documentation for ?as.POSIXct shows the useage for a numeric object

S3 method for class 'numeric'

as.POSIXlt(x, tz = "", origin, ...)

showing you need to supply the origin

Upvotes: 2

user8497316
user8497316

Reputation: 1

This should work.

as.POSIXct(yourNumeric)

Where yourNumeric is your number.

Upvotes: -1

Related Questions