如何比较不带年份的 Rails 中的日期?
我正在尝试找到未来两周内生日的所有用户。
我尝试了以下方法:
User.find(:all, :conditions => ["DOB > ? and DOB <= ?", Date.today, 2.weeks.from_now])
这显然不起作用,因为 DOB 中的“年份”不等于今年。
我只需要比较月份和日期。
我该怎么做呢?
I'm trying to find all users with birthdays in the coming 2 weeks.
I tried the following:
User.find(:all, :conditions => ["DOB > ? and DOB <= ?", Date.today, 2.weeks.from_now])
which obviously doesn't work because the 'year' in the DOB doesn't equal to this year.
I need to only compare the months and the days.
How can I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须在您的 dob 列上执行 mysql 方法
,如下所示
您可以使用 WEEK 等适合您的函数参考 this 用于
ruby 中的mysql 函数
You have to perform mysql method on your dob column
Something like following
You can use functions like WEEK whichever works for you Ref this for mysql function
In ruby
根据 Salil 的回答,我得出以下结论以使其适用于 sqlite:
Based on Salil's answer, I have derived the following to make it work for sqlite:
postgresql 的另一种方法:
Another aproach for postgresql:
这可能有助于更改比较的日期格式,因为默认情况下它将采用 Date.today.year。
Date.strptime("{ 9, 10 }", "{ %m, %d }")
如果您获得一些用于直接比较的代码,则给出 Do post 。
This may help in changing the date format for comparison as by default it will take Date.today.year.
Date.strptime("{ 9, 10 }", "{ %m, %d }") gives
Do post if you get some code for direct comparison.