LoginSignup
0
0

More than 5 years have passed since last update.

Rでdate-timeクラスオブジェクトからUnixtimeに変換

Last updated at Posted at 2014-03-05

strptimeなどで生成したdate-timeクラスオブジェクトをUnixtimeに変換するにはdifftime()で1970-01-01 00:00:00からの差分を計算すればよい。

> hoge <- strptime("2014-03-01 00:00:00",format="%Y-%m-%d %H:%M:%S")
> difftime(hoge,strptime("1970-01-01 00:00:00",format="%Y-%m-%d %H:%M:%S"),units="secs")
Time difference of 1393632000 secs

このあまだと数値として扱えないのでas.numeric()につっこむ。

> hoge.ut <- as.numeric(difftime(hoge,strptime("1970-01-01 00:00:00",format="%Y-%m-%d %H:%M:%S"),units="secs"))
> hoge.ut
[1] 1393632000

なんだかもっとスマートな方法がある気がする。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0