Search code examples
rlubridate

Convert Javascript new Date().getTime(); into a R recognised timestamp (ideally with lubridate)


If I open the Javascript console and type:

new Date().getTime();

I get: 1607534517803

I have a data frame in R with timestamps of this format. I'd like to convert this into a meaningful datetime.

Tried:

lubridate::as_datetime(1607534517803)
[1] "52910-09-28 14:03:23 UTC"

Expected today's date and time (At the time of posting, 12/9/20 at 11:24am USA central time)

How can I return a meaningful timestamp in R?


Solution

  • Try any of these options:

    #Code1
    as.POSIXlt.POSIXct(1607534517803/1000,origin = "1960-01-01")
    
    #Code2
    as.POSIXct(1607534517803/1000,origin = "1960-01-01")
    

    The OP solution (many thanks for hard working):

    #Code2
    strtime_to_dt <- function(x) {   as.POSIXct(as.numeric(x) / 1000, origin = "1960-01-01", formats = "%Y-%m-%d %H:%M:%OS") }