0
0

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 3 years have passed since last update.

Rubyで時間の差(HH:MM:SS)を計算する

Posted at

時間の差をHH:MM:SSとして欲しい時に、変換する際にJSTとして+9:00されてしまって困ったので備忘録

こんな感じ↓
6時間10分ー4時間=11時間10分(JST)+9:00

足し算引き算はunixtimeで数字として行う(これは他に記事いっぱいあるのでそっち見て)

    # 時間の差を見たいので引き算
    diff = Time.zone.at(unix_base_time) - Time.zone.at(something_unix_time)
    p diff.class # FloatとかActiveSupport::TimeWithZoneならいけることを確認した
    p Time.zone.at(diff).strftime('%H:%M:%S') # "09:10:00"
    p Time.zone.at(diff).utc.strftime('%H:%M:%S') # "02:10:00"
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?