Ruby年 - 月 - 上周日的日期

Lui*_*igi 4 ruby time ruby-on-rails

我需要以2014-01-20最后两个星期日的格式计算.

所以,last_sunday = 2014-01-20most_recent_sunday = 2014-01-26.

我使用的是Rails 4.0和Ruby 2.0.我怎样才能得到这些日期?

Kie*_*ews 6

在轨道4:

most_recent_sunday = Time.now.sunday.to_s

last_sunday =        Time.now.last_week.sunday.to_s
Run Code Online (Sandbox Code Playgroud)

要使其达到您所追求的格式:

DateTime.parse(most_recent_sunday).strftime("%Y-%m-%d")
Run Code Online (Sandbox Code Playgroud)

http://apidock.com/rails/v4.0.2/DateAndTime/Calculations/sunday http://apidock.com/rails/v4.0.2/DateAndTime/Calculations/last_week