我有一个营业日期。我想要创建已经获得的期票:
day=1
month= the next month of operation_date
year= automatic
示例:
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
如果存在四张期票
我怎么能做到?
PD:对不起我的语法
答案 0 :(得分:141)
你可以做到
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
等等......
答案 1 :(得分:10)
如果你没有使用rails
或者不想要active_support
,我发现没有active_support的更简单方法
(Date.today - Date.today.mday + 1) # First day of the current month
=> Thu, 01 Dec 2016
希望这有帮助! : - )
答案 2 :(得分:7)
我正在使用带有1.8.7
的{{1}}和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
这个链接可以帮助你http://pleac.sourceforge.net/pleac_ruby/datesandtimes.html。
我不确定ruby中Time.now
方法的可用性,但它确实存在于RoR中。
答案 3 :(得分:7)
N个月的一般回复
(Date.today + N.months).at_beginning_of_month
答案 4 :(得分:6)
我的解决方案:
class Time
def start_of_next_month
Time.local(self.year + self.month / 12, self.month % 12 + 1)
end
end
答案 5 :(得分:4)
对于那些使用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
我知道这有点笨拙:)。
答案 6 :(得分:2)
我想获得一年中的第一个星期一(用于导轨装置),以下工作:
Date.new(Date.today.year,1,1).beginning_of_week
如果您不在轨道中,则可能如下所示,
# 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
答案 7 :(得分:1)
截至2017年11月,最新版本不再支持for (index, dict) in self.appDelegate.arrayOfDictionary.enumerated() {
for element in dict {
if element.key == "sellingPrice" {
let PAinTxtField = alertController.textFields?.first?.text)!
var thePA = (Int(PAinTxtField ) ?? 0) //This is partial amount
let theSP = (Int("\(element.value)") ?? 0) //This is selling price
if thePA < theSP {
self.remainingAmt = theSP - thePA
var dictCopy = dict
dictCopy["remaining_balance"] = "\(self.remainingAmt)" //The mistake seems to be because of this line. But cannot figure out what exactly...:(
}
}
}
}
。考虑到您使用的是Rails,可以使用at_beginning_of_month
答案 8 :(得分:0)
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
答案 9 :(得分: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]
答案 10 :(得分:0)
如果您在Rails App中尝试使用extended_date
这个宝石,则变得很容易。
这将帮助我们获得一个月中任何一天的首次出现
Date.today.first_day_of_month("friday")
Date.today.first_day_of_month(5)
Date.today.first_day_of_month(0)