3

I've found out how to convert a Stata datetime format from milliseconds since Jan 1960 in R from a related question (see below):

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

I am looking to do the opposite in R: i.e. given a datetime expressed as a character string, find out how to return the datetime value as milliseconds since 01 Jan 1960 00:00:00. Any suggestions would be much appreciated.

4

1 回答 1

2

用于as.numeric将日期时间强制转换为自纪元以来的秒数。由于 R 使用 1970 作为其原点,因此您必须另外考虑 1960-1970 偏移量。最后,当然,要注意秒到毫秒的转换。

> mydate = as.POSIXct(874022400000/1000, origin="1960-01-01")
> 1000 * (as.numeric(mydate) - as.numeric(as.POSIXct('1960-01-01')))
[1] 874022400000
于 2012-04-13T22:37:16.693 回答