1

I'm trying to display a message depending on the time of the day and I currently have this piece of code:

<% if Time.now >= "7:00" and Time.now <= "14:00" %>
Good Morning <%= current_user.nome %>! <br>
<% else %>
Good Afternoon <%= current_user.nome %>!
<% end %>

It's currently 14:30 and the page still displays Good Morning (username), when it should display Good Evening (username)

  • Is my code wrong?
  • How do I fix this?

5 Answers5

2

try:

<% if Time.now >= Time.parse("7:00") and Time.now <= Time.parse("14:00") %>
  Good Morning <%= current_user.nome %>! <br>
<% else %>
  Good Afternoon <%= current_user.nome %>!
<% end %>

Note: Time.now will return time object while "7:00" is a string, so you need to convert string time to time object and can compare.

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
2

You can write like this Instead of Parsing the Time you can write simply

Solution : 1

<% if Time.now.hour >= 7 && Time.now.hour <= 14 %>
Good Morning <%= current_user.nome %>! <br>
<% else %>
Good Afternoon <%= current_user.nome %>!
<% end %>

Solution : 2

you can write less code via using ternary operator

(0..11).include?(Time.now.hour) ? "Good Morning <%= current_user.nome %>" : Good Afternoon <%= current_user.nome %>
vel pradeep.MS
  • 584
  • 5
  • 7
2

I think this is worth it to be a helper method:

# in app/helpers/application_helper.rb
def greeting(name)
  greeting = (7..13).cover?(Time.current.hour) ? 'Good Morning' : 'Good Afternoon'

  "#{greeting} #{name}".strip
end

And in your view:

<%= greeting(current_user.nome) %>!

Another more detailed version that supports uneven times might look like this:

def greeting(name)
  greeting = case Time.current.to_s(:time)
             when ('05:00'..'11:59') then 'Good Morning'
             when ('12:00'..'17:59') then 'Good Afternoon'
             when ('18:00'..'21:59') then 'Good Evening'
             else 'Good Night'
             end
  "#{greeting} #{name}".strip
end
spickermann
  • 100,941
  • 9
  • 101
  • 131
1

I'd use a helper:

def morning?(t = Time.current)
  t.between? t.change(hour: 7), t.change(hour: 14)
end

t is a time instance, defaulting to the current time, for example:

t = Time.current
#=> Tue, 04 Jul 2017 16:30:09 CEST +02:00

change sets one or more of the time's elements. If only :hour is specified, the "smaller" units like minutes and seconds are set to 0:

t.change(hour: 7)  #=> Tue, 04 Jul 2017 07:00:00 CEST +02:00
t.change(hour: 14) #=> Tue, 04 Jul 2017 14:00:00 CEST +02:00

between? finally checks if t is between these values.

Passing the current time as an argument makes it easier to test the method:

morning? Time.parse('06:59') #=> false
morning? Time.parse('07:00') #=> true
morning? Time.parse('14:00') #=> true
morning? Time.parse('14:01') #=> false

Usage:

<% if morning? %>
  Good Morning <%= current_user.nome %>!
<% else %>
  Good Afternoon <%= current_user.nome %>!
<% end %>
Stefan
  • 109,145
  • 14
  • 143
  • 218
1

I would use helpers to store methods and keep views plain.

class ApplicationHelper
  def greeting
    "Good #{time_greeting} #{current_user.nome}"
  end

  def time_greeting
    return "Morning" if Time.now.hour.between?(7, 13)
    "Afternoon"
  end
end

Then you may only use greeting method in your views:

<%= greeting %>
Fercell
  • 111
  • 3