Rails - 获取当前周和月份的开始和结束日期 [英] Rails - Get the current week's and month's start and end dates
问题描述
Time.now
来获取当前时间。现在我想找出当前周和当前月份的srart和结束日期。例如: 如果 Time.now
返回我 -
2012-08-13 15:25:35 +0530
然后,本周的开始和结束日期是:
2012-08-12 - 2012-08-18
然后当月的开始和结束日期是:
2012--08-01 - 2012-08-31
我使用代码实现相同:
Time.now.wday
=> ; 1
Time.now - 1.day
=> 2012-08-12 15:31:44 +0530
Time.now + 5.day
=> 2012-08-19 15:32:38 +0530
等等..但是我没有找到这是一个方便的方法。我确定Ruby on Rails确实有更好的解决方案。任何人都可以提出相同的想法。
而不是使用Time.now
使用以下代码来解决问题
d = Date.today
d.at_beginning_of_week
#Mon,2012年8月13日
d.at_beginning_of_week.strftime
#2012-08-13
d.at_beginning_of_week.strftime(%d )
#13 - >打印日期。
同样,您可以在日期类
I am new to ruby on rails and got stuck in dates. I used Time.now
to get the current time. Now I want to find out the srart and end dates for the current week and current month. For instance:
If Time.now
returns me -
2012-08-13 15:25:35 +0530
Then the start and end dates for the current week are:
2012-08-12 - 2012-08-18
Then the start and end dates for the current month are:
2012--08-01 - 2012-08-31
I am implementing the same using the code:
Time.now.wday
=> 1
Time.now - 1.day
=> 2012-08-12 15:31:44 +0530
Time.now + 5.day
=> 2012-08-19 15:32:38 +0530
and so on.. But I dont find it a convenient way to do this. Iam sure Ruby on Rails does hold a better solution. Can anyone suggest to figure out the same..
Instead of using Time.now
Use the following code to get your queries solved
d = Date.today
d.at_beginning_of_week
#Mon, 13 Aug 2012
d.at_beginning_of_week.strftime
#"2012-08-13"
d.at_beginning_of_week.strftime("%d")
#"13" -> To print the date.
Similarly you can find multiple methods in the Date Class
这篇关于Rails - 获取当前周和月份的开始和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!