在Ruby中获取最后一个月的最后一天 [英] Get last day of the month in Ruby

查看:101
本文介绍了在Ruby中获取最后一个月的最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用args(年,月)创建了新对象Date.new。在创建ruby之后,默认添加了01这个对象的一天。有没有办法添加不是第一天,而是通过作为参数的最后一个月的日期(例如28如果将是02个月或31个,如果将是01个月)?

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) ?

推荐答案

使用 Date.Civil

使用 Date.civil(y,m,d)或其别名 .new(y,m,d),可以创建一个新的日期目的。天(d)和月(m)的值可以是负数,在这种情况下,它们分别从年底和月底开始倒数。

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(2010, 02, -1)
=> Sun, 28 Feb 2010
>> Date.civil(2010, -1, -5)
=> Mon, 27 Dec 2010

这篇关于在Ruby中获取最后一个月的最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆