I need to calculate in the format the 2014-01-20last two Sundays.
2014-01-20
So, last_sunday = 2014-01-20and most_recent_sunday = 2014-01-26.
last_sunday = 2014-01-20
most_recent_sunday = 2014-01-26
I am using Rails 4.0 and Ruby 2.0. How can I get these dates?
In rails 4:
most_recent_sunday = Time.now.sunday.to_s last_sunday = Time.now.last_week.sunday.to_s
To go to the following format:
DateTime.parse(most_recent_sunday).strftime("%Y-%m-%d")
http://apidock.com/rails/v4.0.2/DateAndTime/Calculations/sunday http://apidock.com/rails/v4.0.2/DateAndTime/Calculations/last_week
I suggest you take a look at a chronic stone
require 'chronic' Chronic.parse("last sunday") => 2014-01-26 12:00:00 +0200
You can also use the active_support function to subtract 7.daysfrom the date calculated above =)
7.days
[11] pry(main)> last_sunday = Time.now - (Time.now.wday + 7) * 86400 => 2014-01-19 17:17:19 -0600 [12] pry(main)> most_recent_sunday = Time.now - (Time.now.wday) * 86400 => 2014-01-26 17:18:03 -0600
Ruby:
require 'date' last_two_weeks = (Date.today-13)..Date.today last_two_sundays = last_two_weeks.select &:sunday? puts last_two_sundays #=> 2014-01-19, 2014-01-26
Source: https://habr.com/ru/post/1523977/More articles:Detecting if character in String is a smiley (using Android) - javaWhen the calendar activated the reminder receiver in Android, how can I get the event ID? - androidSaving input history from utop to file - ocamlС# - Поиск ключей словаря и значений поиска в списке - dictionaryAndroid application widget with network connection - androidImageView does not stretch image properly - javaHow can I keep track of users using instagram API without speed limit? - apiIs there any way to get Instagram Hashtag Count? - instagramMutual authentication service client (two-way client certificate authentication) - c #Скалярное значение phpspec в let - phpAll Articles