Converting a datetime character string to a double millisecond value since January 1, 1960

I learned how to convert the Stata date and time format from milliseconds from January 1960 to R from a related question (see below):

as.POSIXct(874022400000/1000, origin="1960-01-01")

I want to do the opposite in R: i.e. with the date expressed as a character string, find out how to return the datetime value in milliseconds from 01 January 1960 00:00:00. Any suggestions would be highly appreciated.

+3
source share
1 answer

Use as.numericto force date-time back to seconds from an era. Since R uses 1970 as its beginning, you must also take into account the 1960-1970 offset. Finally, of course, take care of converting seconds to milliseconds.

> mydate = as.POSIXct(874022400000/1000, origin="1960-01-01")
> 1000 * (as.numeric(mydate) - as.numeric(as.POSIXct('1960-01-01')))
[1] 874022400000
+2

All Articles