Rails日期与Date.today相比 [英] Rails Date compared to Date.today

查看:113
本文介绍了Rails日期与Date.today相比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期格式有一个birth_date变量。我想和Date.today进行比较,如下所示。问题是它回来是虚假的,因为它也想比较一年。这是一个生日,所以我不在乎一年只是想看看birth_date(月和日)是否等于Date.today.day.month。

I have a birth_date variable in the Date format. I want to compare it to Date.today as shown below. The problem is it is coming back false because it wants to compare the year as well. It is a birthday so I don't care about the year just trying to see if birth_date (month and day) is equal to Date.today.day.month.

任何想法?

bdays = Soldier.find(:all, :conditions => ["birth_date LIKE ?", Date.today] )


推荐答案

你将需要分解你的日期,因为你想忽略一年。您将需要使用您的SQL提供程序提供的一些功能(下面的示例使用MySQL):

You will need to break up your date, because you want to ignore the year. You will need to use some of the functions given by your SQL provider (the example below uses MySQL):

bdays = Soldier.find(:all, :conditions => ["DAY(birth_date) = ? AND MONTH(birth_date) = ?", Date.today.day, Date.today.month])

如果您使用SQLite(rails的默认数据库),将会更复杂一些,因为它们没有真正的日期类型:

if you're using SQLite (rails' default database), it will be a bit more complicated because they don't have a real date type:

bdays = Soldier.find(:all, :conditions => ["STRFTIME('%d', birth_date) = ? AND STRFTIME('%m', birth_date) = ?", Date.today.day, Date.today.month])

这篇关于Rails日期与Date.today相比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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