LoginSignup
0
0

More than 5 years have passed since last update.

文字列の時間を数字に変換する

Last updated at Posted at 2018-08-10

ユースケース

CSVに時間が文字列で記載されており、データベースに格納するときに数字にしたい

調査

英語の検索ワードは、「Convert time to seconds?」や「Convert HH:MM:SS to seconds」で調べることができる

結論

86400で割った余り
86400 = 24(時) * 60(分) * 60(秒)

> Time.parse("12:00:00").to_i % 86400
=> 43200

秒がなくても変わらない

> Time.parse("12:00").to_i % 86400
 => 43200 

61秒にするとエラー

> Time.parse("00:00:61").to_i % 86400
=> ArgumentError: argument out of range
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