如何在ruby中获得下个月的第一天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4399857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to obtain first day of next month in ruby?
提问by maxiperez
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
PD:请原谅我的语法
回答by jordinl
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...
等等...
回答by Sagar Ranglani
In case you are not using railsor do not wish to require active_support, I found a simpler way without active_support
如果您不使用rails或不希望 require active_support,我找到了一种没有 active_support 的更简单的方法
(Date.today - Date.today.mday + 1) # First day of the current month
=> Thu, 01 Dec 2016
Hope this helps! :-)
希望这可以帮助!:-)
回答by Jerome
A response to generically obtain N months
一般获得N个月的回应
(Date.today + N.months).at_beginning_of_month
回答by boggy
My solution:
我的解决方案:
class Time
def start_of_next_month
Time.local(self.year + self.month / 12, self.month % 12 + 1)
end
end
回答by ranendra
I am using Ruby 1.8.7and 1.9.2with rvmand in both cases Date.todayresults an error:
我使用Ruby1.8.7和1.9.2与rvm在两种情况下Date.today会导致一个错误:
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.nowand this link should help you http://pleac.sourceforge.net/pleac_ruby/datesandtimes.html.
我认为你应该使用Time.now这个链接应该可以帮助你http://pleac.sourceforge.net/pleac_ruby/datesandtimes.html。
I am not sure about availability of at_beginning_of_monthmethod in ruby but it does exists in RoR.
我不确定at_beginning_of_monthruby中方法的可用性,但它确实存在于 RoR 中。
回答by boggy
for those working with Time class :
对于那些使用 Time 类的人:
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 :)
我知道这有点笨拙:)
回答by Shyam Habarakada
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,
如果你不在轨道上,它可以像下面这样,
# 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
回答by Adam Aiken
As Nov 2017, at_beginning_of_monthis no more supported in the latest version. You can use beginning_of_monthinstead, considering you are using Rails
2017 年 11 月,at_beginning_of_month最新版本不再支持。beginning_of_month考虑到您正在使用 Rails,您可以改用
回答by Mark Reed
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
回答by user12697316
(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]

