在 Ruby 中获取本月的最后一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14120661/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:38:14  来源:igfitidea点击:

Get last day of the month in Ruby

rubydatedatetime

提问by sanny Sin

I made new object Date.new with args (year, month). After create ruby added 01 number of day to this object by default. Is there any way to add not first day, but last day of month that i passed as arg(e.g. 28 if it will be 02month or 31 if it will be 01month) ?

我用 args(年,月)创建了新对象 Date.new。默认情况下,创建 ruby​​ 后向该对象添加了 01 天数。有什么方法可以不添加第一天,而是添加我作为 arg 传递的月份的最后一天(例如,如果是 02month 则为 28,如果是 01month 则为 31)?

回答by Netorica

use Date.civil

Date.civil

With Date.civil(y, m, d)or its alias .new(y, m, d), you can create a new Date object. The values for day (d) and month (m) can be negative in which case they count backwards from the end of the year and the end of the month respectively.

使用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

回答by Thomas Klemm

To get the end of the month you can also use ActiveSupport's helper end_of_month.

要获得月底,您还可以使用 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

You can find out more on end_of_monthin the Rails API Docs.

您可以在 Rails API 文档中找到有关end_of_month 的更多信息。

回答by Jumpers

So I was searching in Google for the same thing here...

所以我在这里在谷歌搜索同样的东西......

I wasn't happy with above so my solution after reading documentation in RUBY-DOCwas:

我对上面的内容不满意,所以在阅读RUBY-DOC中的文档后我的解决方案是:

Example to get 10/31/2014

获取示例 10/31/2014

Date.new(2014,10,1).next_month.prev_day

Date.new(2014,10,1).next_month.prev_day

回答by akostadinov

This is my Timebased solution. I have a personal preference to it compared to Datealthough the Datesolutions proposed above read somehow better.

这是我Time基于的解决方案。Date尽管Date上面提出的解决方案读起来更好,但我对它有个人偏好。

reference_time ||= Time.now
return (Time.new(reference_time.year, (reference_time.month % 12) + 1) - 1).day

btw for December you can see that year is not flipped. But this is irrelevant for the question because december always has 31 day. And for February year does not need flipping. So if you have another use case that needs year to be correct, then make sure to also change year.

顺便说一句,十二月你可以看到那一年没有翻转。但这与问题无关,因为十二月总是有 31 天。而二月年不需要翻转。因此,如果您有另一个需要年份才能正确的用例,请确保也更改年份。

回答by Ali Akbar

require "date"
def find_last_day_of_month(_date)
 if(_date.instance_of? String)
   @end_of_the_month = Date.parse(_date.next_month.strftime("%Y-%m-01")) - 1
 else if(_date.instance_of? Date)
   @end_of_the_month = _date.next_month.strftime("%Y-%m-01") - 1
 end
 return @end_of_the_month
end

find_last_day_of_month("2018-01-01")

This is another way to find

这是另一种查找方式

回答by Даниэль Сайфулин

You can do something like that:

你可以这样做:

def last_day_of_month?
   (Time.zone.now.month + 1.day) > Time.zone.now.month
end

Time.zone.now.day if last_day-of_month?