如何从周数中获取日期? [英] How do I get a Date from a week number?

查看:40
本文介绍了如何从周数中获取日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查找在给定周内创建的所有项目,并传入周数参数.(created_at 是一个普通的时间戳.)

I want to find all items created in a given week, and pass in a week number param. (created_at is a normal timestamp.)

给定周数,在该特定周内查找日期的最简单方法是什么?(一周中的任何日期都可以,因为我将在范围内使用 begin_of_week 和 end_of_week.)

Given a week number, what is the easiest way to find a date in that particular week? (Any date in the week will do, as I will use beginning_of_week and end_of_week in the scope.)

推荐答案

您可以使用 commercial 方法获取表示一周开始和结束的 Date 对象:

You can get Date objects representing the beginning and end of your week using the commercial method:

week = 41;

wkBegin = Date.commercial(2010, week, 1)
wkEnd = Date.commercial(2010, week, 7)

现在做你的发现:

  Item.find(:all, :conditions->:create_date=>wkBegin..wkEnd.end_of_day)

这篇关于如何从周数中获取日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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