san*_*Sin 49 ruby datetime date
我用args(年,月)创建了新对象Date.new.创建ruby后,默认情况下为此对象添加01天.有没有办法添加不是第一天,而是我作为arg传递的月份的最后一天(例如28如果它将是02个月或31如果它将是01个月)?
Mp *_*ega 92
使用 Date.civil
使用Date.civil(y, m, d)
或别名.new(y, m, d)
,您可以创建一个新的Date对象.日(d)和月(m)的值可以是负的,在这种情况下,它们分别从年末和月末倒计时.
=> Date.civil(2010, 02, -1)
=> Sun, 28 Feb 2010
>> Date.civil(2010, -1, -5)
=> Mon, 27 Dec 2010
Run Code Online (Sandbox Code Playgroud)
Tho*_*emm 63
要获得月末,您还可以使用ActiveSupport的帮助程序end_of_month
.
# Require extensions explicitly if you are not in a Rails environment
require 'active_support/core_ext'
p Time.now.utc.end_of_month # => 2013-01-31 23:59:59 UTC
p Date.today.end_of_month # => Thu, 31 Jan 2013
Run Code Online (Sandbox Code Playgroud)
您可以在Rails API文档中的end_of_month上找到更多信息.
归档时间: |
|
查看次数: |
35565 次 |
最近记录: |