23
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Ruby] 日付をStringにしたりTimeにしたりUnixTimestampにしたり

Last updated at Posted at 2014-04-23

API経由で取得した日付情報(String)を扱う時とか。
いつも忘れるのでメモ。

String to Time

Time.parse()

# Time to String
> str = Time.now.to_s
=> "2014-04-07 10:00:00"

# String to Time
> time = Time.parse(str)
=> 2014-04-07 10:00:00 +0900

UnixTimestamp to Time

Time.at()

# Time to UnixTimestamp
> unixtime = Time.now.to_i
=> 1396832400

# UnixTimestamp to Time
> time = Time.at(unixtime)
=> 2014-04-07 10:00:00 +0900
23
21
1

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
23
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?