简体   繁体   中英

Rails get Date part of DateTime with Time Zone

I'm trying to execute a query in rails 3 with the following syntax: CourseOrder.where("DATE(ordered_at) = ?", date) .

My problem is that rails saves all times in UTC, so in my Timezone (+2) at 0:33 its the 4th but in UTC its the 3th of the month. Is there a way to query the date part of a DateTime with Timezone?

Don't use date, do it like this instead:

time = DateTime.civil_from_format(:local, 2011, 4, 9)
CourseOrder.where("ordered_at > ? AND ordered_at < ?", time - 1.day, time)

You're storing a datetime, so query that instead of trying to use date, which doesn't account for Timezones.

solved the problem with a change in which values I compare.

I have a Date value to, and now I'm comparing a date value with a date value which is better than comparing date value with datetime value which have to fail. thanks for leading me to the right direction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM