LoginSignup
2
0

More than 3 years have passed since last update.

Rails | at_beginning_of_month 日付操作 月初, 翌月の月初、先月の月初の取得

Posted at

月初を取得

#月初を取得
Date.today.at_beginning_of_month
#=> Wed, 01 Dec 2010

翌月の月初を取得

#翌月の月初を取得
Date.today.at_beginning_of_month.next_month
#=> Sat, 01 Jan 2011

翌々月の月初を取得

Date.today.at_beginning_of_month.next_month.next_month
#=> Tue, 01 Feb 2011

先月の月初を取得

#先月の月初を取得
Date.today.at_beginning_of_month.prev_month
#=> Mon, 01 Mar 2021

4/1 - 4/15と、先月の同期間(3/1 - 3/15)の比較

@last_month_conversions = Conversion.where(conversion_date: Date.today.Date.today.at_beginning_of_month.prev_month..Date.today.prev_month
)
@this_month_conversions = Conversion.where(conversion_date: Date.today.Date.today.at_beginning_of_month..Date.today)
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