Damir Nurgaliev
Damir Nurgaliev

Reputation: 351

How to get the date by day of the week ruby

I'm creating rails app, and I need to put calendar into it. I have a json data which sending to app once in a week. JSON object has something like this:

route: "Monday"

or

route: "Saturday"

I need to put this day of the week as usual date(date calculated every week, since the day when the JSON was sent), for example JSON objects sent today, and to all of them I should give a date, routes: "Saturday" should give me 24-June-2017. Hope u understand my question :)

Upvotes: 3

Views: 5988

Answers (3)

broken_historian
broken_historian

Reputation: 37

When working with any built in packages in Ruby it is always a good idea to check out the documentation for available functions and examples. For time, Ruby comes with a convenient Time object which simplifies much of the interactions we do with Time.

For example, one can get the current time using:

t = Time.now, one can then do (t + 1.days).monday? or (t + 2.days).day == 1.

Days already have corresponding int values.

http://ruby-doc.org/core-2.4.1/Time.html

This documentation applies to the newest head, but you can find the version for your ruby with ruby -v and find the appropriate version because it does change over versions.

For your issue, you could have the URL use the string Monday and using a switch case or hash to derive the integer difference from today, using this one could get the exact calendar date. Once it is parsed to Time, you could also do Time.monday?..Time.sunday?, which you may or may not find useful when trying to solve your problem. There are many ways to approach it, but difference from Today is a good solution and with how Time is able to add days like Time.now + 7.days, it feels natural using Ruby to do it in this way.

Upvotes: 1

Kruupös
Kruupös

Reputation: 5474

In addition to @TomLord answer:

One can check which day of the week it is, by using dedicated method monday? to saturday?

e.g:

Date.today
# => Mon, 03 Jul 2017
Date.today.monday?
# => True
Date.today.saturday?
# => False

Upvotes: 2

Tom Lord
Tom Lord

Reputation: 28285

To get the current day in Rails, you can use:

Date.today

To get the previous Saturday (since today is Monday 3rd July, that's Saturday 1st July), you can use:

Date.today.beginning_of_week(:saturday)

Or, if what you actually wanted was the previous week's Saturday (Saturday 24th June), then you can use:

Date.today.weeks_ago(1).beginning_of_week(:saturday)

Or, if you prefer:

1.week.ago.beginning_of_week(:saturday)

...However, note that the above will return an ActiveSupport::TimeWithZone object rather than a Date object - so will behave slightly differently.

Have a read through the rails documentation - in particular, the ActiveSupport extensions to ruby's Date class to see what methods are available.

If, for some reason, you needed to do this in pure ruby (i.e. without the above mentioned ActiveSupport extensions that come bundled with rails), then you could instead utilise the Date.parse method:

require 'date'

Date.parse("Saturday") - 7
  # => Sat, 01 Jul 2017

In order to convert your Date (or similar) object to the string format you desire ("24-June-2017"), you can use ruby's strftime method:

(Date.parse("Saturday") - 7).strftime('%d-%B-%Y')

In rails, it is a common convention to place such custom date formats in a locale or initializer to avoid repetition. You can then reference this format by name:

# config/initializers/date_formats.rb
# (Call this whatever you want!)
Date::DATE_FORMATS[:calendar] = '%d-%B-%Y'

# Anywhere else in the rails app
Date.today.beginning_of_week(:saturday).to_s(:calendar)
  # => "01-July-2017"

Upvotes: 8

Related Questions