ruby-on-rails - rails : Is there away to get the Date object that is the closest Monday to today?

标签 ruby-on-rails date

给定日期,如何在 Rails 中找到最近的星期一?

我知道我可以做以下事情:

日期.明天
日期.今天

有类似 Date.nearest :monday 的东西吗?

最佳答案

commercial Date 对象上的方法将允许您执行此操作。这个例子将在下周一为您提供帮助。

Date.commercial(Date.today.year, 1+Date.today.cweek, 1)

如果您需要下一个或上一个星期一(以最接近的为准),您可以执行以下操作:
Date.commercial(Date.today.year, Date.today.cwday.modulo(4)+Date.today.cweek, 1)

我现在不能执行这个,所以如果有语法错误,请原谅我。

关于ruby-on-rails - rails : Is there away to get the Date object that is the closest Monday to today?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4514988/

相关文章:

ruby-on-rails - 使用 FFaker::Internet.user_name 时指定最小长度

ruby-on-rails - 使用 Assets 管道从 s3 提供 GZipped 文件

javascript - 当日期来自不同时区时,在 IOS 中为 angularjs 项目返回无效日期

node.js - new Date() 如何返回比之前调用 new Date() 更早的时间(在 Node.js 中)?

java - 如何按今天/本周/本月对日期数组进行排序 - android

mysql - SQL/MySQL : Retrieve active article from table with 3 columns (Old | New | Date)

mysql - 在没有 key 长度的 key 规范中使用的 Rails BLOB/TEXT 列

ruby-on-rails - ruby on rails 闪存消息 - :alert :error :notice and :success?

date - 当前日期减一个月 Crystal 报告

ruby-on-rails - 如何为 Ruby On Rails 设置 Postfix 以发送电子邮件