如何用ruby获得下个月的第一天?

时间:2022-10-16 09:32:09

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

打扰一下,我的语法

9 个解决方案

#1


132  

You can do

你可以做

require "active_support"

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...

等等……

#2


7  

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

我在rvm中使用Ruby 1.8.7和1.9.2,这两种情况都是日期。今天结果一个错误:

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 .

我想你应该利用时间。现在,这个链接应该可以帮助您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.

我不确定是否可以在ruby中使用at_beginning_of_month方法,但它确实存在于RoR中。

#3


6  

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

如果您不使用rails或不希望需要active_support,我找到了一种没有active_support的更简单的方法

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

Hope this helps! :-)

希望这可以帮助!:-)

#4


5  

A generic response for N months

一般反应N个月

(Date.today + N.months).at_beginning_of_month

#5


5  

My solution:

我的解决方案:

class Time
  def start_of_next_month
    Time.local(self.year + self.month / 12, self.month % 12 + 1)
  end
end

#6


4  

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 :)

我知道有点笨拙:

#7


2  

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

我想要得到今年的第一个星期一(一个rails夹具)和以下工作:

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

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

如果你不在rails中,可以如下所示,

# 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

#8


1  

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

到2017年11月,最新版本不再支持at_beginning_of_month。考虑到您正在使用Rails,您可以使用beginning_of_month

#9


0  

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

ActiveSupport似乎有点重量级。我会这样做:

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 

#1


132  

You can do

你可以做

require "active_support"

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...

等等……

#2


7  

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

我在rvm中使用Ruby 1.8.7和1.9.2,这两种情况都是日期。今天结果一个错误:

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 .

我想你应该利用时间。现在,这个链接应该可以帮助您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.

我不确定是否可以在ruby中使用at_beginning_of_month方法,但它确实存在于RoR中。

#3


6  

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

如果您不使用rails或不希望需要active_support,我找到了一种没有active_support的更简单的方法

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

Hope this helps! :-)

希望这可以帮助!:-)

#4


5  

A generic response for N months

一般反应N个月

(Date.today + N.months).at_beginning_of_month

#5


5  

My solution:

我的解决方案:

class Time
  def start_of_next_month
    Time.local(self.year + self.month / 12, self.month % 12 + 1)
  end
end

#6


4  

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 :)

我知道有点笨拙:

#7


2  

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

我想要得到今年的第一个星期一(一个rails夹具)和以下工作:

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

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

如果你不在rails中,可以如下所示,

# 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

#8


1  

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

到2017年11月,最新版本不再支持at_beginning_of_month。考虑到您正在使用Rails,您可以使用beginning_of_month

#9


0  

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

ActiveSupport似乎有点重量级。我会这样做:

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