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

[備忘録]RailsでJson形式で渡ってきた日付をDate型にparseする

Last updated at Posted at 2017-06-16

タイトル通りです。

json形式で渡ってきた日付の値を基に、日付計算(beginning_of_day、加減算)とかしたかったのでjson形式(文字列)で渡ってきた値をDate型に変換する必要がある。

date =  Time.zone.parse(params[:date_param]) #json系式で渡ってきたdate_paramをrailsのタイムゾーンに合わせてパース
p params[:date_param]   # => 2017-06-16T02:59:28.928Z
p date                  # => 2017-06-16 11:59:28 +0900
p date.beginning_of_day # => 2017-06-16 00:00:00 +0900
User.where(created_at:date.beginning_of_day..date.end_of_day) # 指定された日で作成されたdateを抽出

これでjson形式で渡ってきたのを日付演算できました。

2
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
2
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?