我有一个营业日期.我想要创建已经获得的期票:
day=1
month= the next month of operation_date
year= automatic
Run Code Online (Sandbox Code Playgroud)
示例:
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
Run Code Online (Sandbox Code Playgroud)
如果存在四张期票
我该怎么做?
PD:对不起我的语法
jor*_*inl 157
你可以做
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
Run Code Online (Sandbox Code Playgroud)
等等...
Sag*_*ani 13
如果您不使用rails或不想要求active_support,我发现没有active_support的更简单的方法
(Date.today - Date.today.mday + 1) # First day of the current month
=> Thu, 01 Dec 2016
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!:-)
N个月的一般回复
(Date.today + N.months).at_beginning_of_month
Run Code Online (Sandbox Code Playgroud)
我的解决方案
class Time
def start_of_next_month
Time.local(self.year + self.month / 12, self.month % 12 + 1)
end
end
Run Code Online (Sandbox Code Playgroud)
我使用Ruby 1.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
Run Code Online (Sandbox Code Playgroud)
我应该使用的东西Time.now
,这个链接应该帮助你http://pleac.sourceforge.net/pleac_ruby/datesandtimes.html.
我不确定at_beginning_of_monthruby 中方法的可用性,但它确实存在于RoR中.
对于那些使用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
Run Code Online (Sandbox Code Playgroud)
我知道这有点笨拙:)
| 归档时间: |
|
| 查看次数: |
45900 次 |
| 最近记录: |