English 中文(简体)
我如何在鲁比拉工作几天和几个月?
原标题:How do I work with days and months in Ruby?

I have the date when an article was created in a DB table. I have articles since the August until today.

I tried to create a SQL query, in ActiveRecord, to get the articles that were created today.

例如,今天是1月14日,我试图将8月14日——9月14日——第14天——产生的条款提交今天。

我试图这样做:

today = Date.today
day = today.strftime( %d )
day_next = (today + 1.day).strftime( %d )

Article.where( created_at >= ? AND created_at <= ? , day, day_next) #day = 14, day_next = 15

但是,这并没有回到正确的说法,因为文章已于12月14日和10月14日发表。 这使我不满意。

最佳回答

You re overthinking this. Assuming created_at is a normal datetime/timestamp field you should just convert created_at to its day using DAY() and then compare it directly to a normal Ruby Date object.

day = Date.today

Article.where  DAY( created_at ) = DAY( ? ) , day

# or just...

Article.where  DAY( created_at ) = DAY( CURRENT_DATE() ) 

(回答你的问题;回答最新情况)

问题回答

暂无回答




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签