67

I have a date of operation. I want create promissory notes which have got:

day=1
month= the next month of operation_date
year= automatic

Example :

operation_date = 15/11/2010 
promissory_note1_date = 1/12/2010
promissory_note2_date = 1/01/2011
promissory_note3_date = 1/02/2011
promissory_note4_date = 1/02/2011

if exist four promissory notes

How could I make it?

PD: Excuse me my syntax

notapatch
  • 6,569
  • 6
  • 41
  • 45
maxiperez
  • 1,470
  • 2
  • 20
  • 40

11 Answers11

203

You can do

require "active_support"

require "active_support/core_ext/date/calculations"
# || require "active_support/all"

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

And so on...

References

Rails guide 3.0 - 6.1.4 - ActiveSupport - How to Load Core Extensions
Rails guide 3.0 - 6.1.4 - ActiveSupport - Extensions to date

notapatch
  • 6,569
  • 6
  • 41
  • 45
jordinl
  • 5,219
  • 1
  • 24
  • 20
  • 31
    `at_beginning_of_month` and so on are provided by ActiveSupport, and are thus available in Rails, but not in pure Ruby. – iGEL Oct 08 '12 at 09:03
  • 4
    Just for clarification: this is not pure Ruby but part of the Rails extensions, you need `require "active_support/core_ext"` to make this works – fguillen Mar 16 '13 at 20:30
  • 1
    Btw, [`Date.today.beginning_of_month`](https://api.rubyonrails.org/classes/DateAndTime/Calculations.html#method-i-beginning_of_month) works, too. – software_writer Feb 01 '23 at 03:43
19

In case you are not using rails or do not wish to require active_support, I found a simpler way without active_support

(Date.today - Date.today.mday + 1) # First day of the current month
=> Thu, 01 Dec 2016

Hope this helps! :-)

Sagar Ranglani
  • 5,491
  • 4
  • 34
  • 47
  • 3
    Nice, thanks. And without the +1 it's the last of the previous month, which is also nice :) – bo-oz Jan 07 '17 at 21:51
12

A response to generically obtain N months (in rails)

(Date.today + N.months).at_beginning_of_month
Jerome
  • 5,583
  • 3
  • 33
  • 76
8

My solution:

class Time
  def start_of_next_month
    Time.local(self.year + self.month / 12, self.month % 12 + 1)
  end
end
Karol Selak
  • 4,248
  • 6
  • 35
  • 65
boggy
  • 192
  • 2
  • 4
7

I am using Ruby 1.8.7 and 1.9.2 with rvm and in both cases Date.today results an error:

ruby-1.8.7-p302 > Date.today
NameError: uninitialized constant Date

ruby-1.9.2-p0 > Date.today
NameError: uninitialized constant Object::Date

I thing you should use Time.now and this link should help you http://pleac.sourceforge.net/pleac_ruby/datesandtimes.html .

I am not sure about availability of at_beginning_of_month method in ruby but it does exists in RoR.

shybovycha
  • 11,556
  • 6
  • 52
  • 82
ranendra
  • 2,492
  • 2
  • 19
  • 29
5

for those working with Time class :

class Time
        def start_of_next_month
                t = Time.local(self.year,self.month)+45*24*3600;
                Time.local(t.year,t.month)
        end
end

I know it's little clumsy :)

boggy
  • 192
  • 2
  • 4
3

I wanted to get the first monday of the year (for a rails fixture) and the following worked:

Date.new(Date.today.year,1,1).beginning_of_week

If you aren't in rails, it can be like below,

# get the last day of the previous year
d = Date.new(Date.today.year - 1,12,31)
# advance to the next monday. This relies on w.day being 0..6, where 0 is sunday.
first_monday = d + (8 - d.wday).days
Shyam Habarakada
  • 15,367
  • 3
  • 36
  • 47
2

As Nov 2017, at_beginning_of_month is no more supported in the latest version. You can use beginning_of_month instead, considering you are using Rails

Adam Aiken
  • 418
  • 7
  • 18
  • 1
    Thanks! And to save the googling for the next person. This is the apidock link for this method as of Rails 6.0.0 https://apidock.com/rails/v6.0.0/DateAndTime/Calculations/beginning_of_month – Jay Killeen Sep 03 '20 at 10:50
1

ActiveSupport seems a little heavyweight to load for this. I would do it this way:

require 'date'
first = Date.new(Date.today.year, Date.today.month, 1)
4.times do |m|
  puts "Promissory note due on #{first >> (m + 1)}"
end 
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
0
 (DateTime.now.beginning_of_year..DateTime.now.end_of_year).to_a.select {|k| k if k == k.beginning_of_month}
=> [Wed, 01 Jan 2020 00:00:00 +0530, Sat, 01 Feb 2020 00:00:00 +0530, Sun, 01 Mar 2020 00:00:00 +0530, Wed, 01 Apr 2020 00:00:00 +0530, Fri, 01 May 2020 00:00:00 +0530, Mon, 01 Jun 2020 00:00:00 +0530, Wed, 01 Jul 2020 00:00:00 +0530, Sat, 01 Aug 2020 00:00:00 +0530, Tue, 01 Sep 2020 00:00:00 +0530, Thu, 01 Oct 2020 00:00:00 +0530, Sun, 01 Nov 2020 00:00:00 +0530, Tue, 01 Dec 2020 00:00:00 +0530]
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

This gem extended_date makes it easy, if you are trying it in Rails App. This will help us to get first occurrence of any day in a month

Date.today.first_day_of_month("friday") Date.today.first_day_of_month(5) Date.today.first_day_of_month(0)

Prem
  • 5,844
  • 3
  • 25
  • 23