1時間前
1時間後
DateをTimeに変換する
文字列をTimeに変換する
"2019-03-01 23:33:17".to_time
※ to_datetimeでも同じことができるが、time zoneがUTCになってしまうので注意
文字列からTimeオブジェクトを取得する
Time.zone.parse("2019-03-01 23:33:17")
[参考]
現在時刻を指定のフォーマットで取得する
today = Time.current.strftime('%Y-%m-%d %H:%M:%S')
今日の始まりの日時をとる
DateTime.now.beginning_of_day
今日の終わりの日時を取る
今週の始まりの日を取る(月曜日始まり)
DateTime.now.beginning_of_week
次月の取得
今日の特定の時間を取得する(時間を操作する)
target_time = Time.now.change(min: 0, sec: 0)
[参考]
特定のフォーマットで出力する
data.created.strftime("%Y-%m-%d %H:%M:%S")
unixtimeをtimeオブジェクトにする
エポックmsへの変換
data.created_at.strftime('%s%L').to_i
GMTの文字列をJSTのTimeオブジェクトに変換する(railsのみ)
time = "2018-08-14 01:37:03 GMT +00:00".in_time_zone
=> Tue, 14 Aug 2018 10:37:03 JST +09:00