鉴于前几天没提到的rails中的时间/日期常用方法,补充下面的方法。
rails中常用的取时间节点的方法,对于计算天数/时间很有帮助的。
Time.now.at_beginning_of_day Time.now.at_beginning_of_quarter Time.now.at_beginning_of_month Time.now.at_beginning_of_year Time.now.at_midnight
上面取到的时间都是00:00结尾,下面是例子
Time.now.at_beginning_of_day
=> Wed Sep 30 00:00:00 +0800 2015
Time.now.at_end_of_week Time.now.at_end_of_month Time.now.at_end_of_quarter Time.now.at_end_of_year Time.now.end_of_day Time.now.end_of_month Time.now.end_of_quarter Time.now.end_of_week Time.now.end_of_year
上面取到的是已59:59结尾的,下面是例子
Time.now.at_end_of_week
=> Sun Oct 04 23:59:59 +0800 2015
下面的取当前对应时间的某年月日
Time.now.last_year Time.now.prev_year Time.now.next_year Time.now.last_month Time.now.prev_month Time.now.next_month Time.now.yesterday Time.now.tomorrow
而下面这个取week的有点特殊
Time.now.next_week
=> Mon Oct 05 00:00:00 +0800 2015
取得是下周的开始时间,或许以后rails更新后会改变,不过也可以用Time.now + 1.week
获得同样的结果。
补充一个设定指定时间的方法
Time.mktime(2015,9,30,17,18,19)
=> Wed Sep 30 17:18:19 +0800 2015