I'm trying to learn some rails and it's moving forward, but something that bothers me is as the title says, the ending of the timestamp. As im swedish i modified it to display my correct timezone.
EG: 2014-08-20 13:24:51 +0200
But what i want to see is 2014-08-20 13:24
To display the time for each post i use
<% #blog_posts.reverse_each do |blog_post| %>
<h3><%= blog_post.title %></h3>
<p> <%= blog_post.created_at.to_s %> </p>
<p>
<%= simple_format(blog_post.body) %>
</p>
<% end %>
Anyone with a simple idea to solve the issue?
In Rails you can use the to_time function on a string to convert it into a Date object:
> '2014-8-20 14:27:46'.to_time.strftime('%B %e at %l:%M %p')
=> "August 20 at 2:27 PM"
strftime Format Directives:
%B Full month name
%e Day of the month
%l Hour of the day
%M Minute of the hour
%p Meridian indicator (AM/PM)
Try
<%= blog_post.created_at.to_s %>
Or use strftime:
<%= blog_post.created_at.strftime("%Y-%m-%d %H:%M") %>
You can use to_formatted_s method:
<%= blog_post.created_at.to_formatted_s(:db) %>
It will show seconds also. If you want only date, hours and minute, try:
<%= blog_post.created_at.strftime("%Y-%m-%d %H:%M") %>
Look at method strftime from ruby class Time
<%= blog_post.cteated_at.strftime("%Y-%m-%d %H-%M") %>
This link could be helpfull too
Strfti.me
EDIT:
For localize your time you must use localize or l => <%= l post.created_at %>
first download yours locale yml file from Rails locale files
copy the raw file to the conifg/locales.
In this file you could set yours default strftime:
formats:
default: "%B %e at %l:%M %p"
Then in your application.rb
config.i18n.default_locale = :pl // for me is pl :)
and finally in your view
l blog_post.created_at
Hope that help you :)
try this..
<%= #user.created_at.to_date.to_formatted_s(:long_ordinal)%> => July 5th, 2014
<%=#user.created_at.strftime("%b %d,%Y") %> => Jul 05,2014
...whichever u prefer
Related
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?
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.
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 %>
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
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 %>
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 %>
I’m trying to compare a saved date_field with today’s date. Seems to work in console but I can't make it work in my code.
<% #flights.each do |flight| %>
<% if (Time.new("%Y-%m-%d") < flight.flight_date) %>
<tr class="active">
<% else %>
<tr class="success">
<% end %>
Thanks to #drenmi for the patient answers and explaining why that my date_field was saved as a datetime attribute hence Time.now working and not Date.today.
Now to fix my naming so I follow convention!
Using Time.new("%Y-%m-%d") will produce an empty Time object:
Time.new("%Y-%m-%d")
# => 0000-01-01 00:00:00 +0730
What you want to do is compare to Time.now:
<% if Time.now < flight.flight_date %>
Or, if using Rails, you might want to use Time.current, or Date.current:
<% if Date.current < flight.flight_date %>
or
<% if Time.current < flight.flight_date %>
you want using Date.today in rails
Time.now.utc
this is the best choice
time = Time.now.utc.iso8601
Time.iso8601(time)
...
don't forget: require 'time'
I would like to make something like show posts from a particular date.
I have
<% date =Date.parse('2014-06-18') %>
<% if feed_item_users.created_at < (date+1).strftime('%Y-%m-%d') && feed_item_users.created_at > date %>
<li id="<%= feed_item_users.id %>">
<span class="content"><%= wrap(feed_item_users.content) %></span>
<span class="timestamp">
Published: <%= I18n.l feed_item_users.created_at, :format => :h %>
</span>
</li>
<% end %>
in my view I'm checking date.
Now I would like to make a date_time select to get date from my web in format like '2014-06-18' and make default today date.
I know that it should look like that:
<%= form_for(??????) do |f| %>
<div class="field">
<%= f.label :published_at %><br>
<%= f.datetime_select :published_at %>
</div>
<% end %>
but I don't know how to make it to variable because I need it only one time.
You're comparing dates against strings here which isn't reliable. In your ruby code you should always compare dates against dates (or times).
Your logic seems to be "if created at is greater than the start of today and less than the start of tomorrow", which can be massively simplified to "if it's today".
ie you can replace this
<% date =Date.parse('2014-06-18') %>
<% if feed_item_users.created_at < (date+1).strftime('%Y-%m-%d') && feed_item_users.created_at > date %>
with this
<% if feed_item_users.created_at.to_date == Date.today %>
The datetime_select is documented here
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html
strftime returns string so when you apply less then operator it doesn't work.
Better way is to do it like this:
time = Time.new(2014, 06, 18)
if feed_item_users.created_at > time.at_beginning_of_day && feed_item_users.created_at < (time+1.day).at_beginning_of_day
I am trying to split the value returned by the created_at method.
I am taking an object (item) and getting the time it was created at. I don't want the whole result and I am only interested in the day, month and year. How can I get this to work?
<% #items_in_basket.each do |item| %>
<% splliter = item.created_at.split(" ") %>
<% time = #splliter[0] %>
I get this as a result:
undefined method 'split' for Wed, 31 Jul 2013 16:44:37 UTC +00:00:Time
created_at returns an instance of ActiveSupport::TimeWithZone which you can simply call day, month, or year on to get those parts of the date. Example:
<% #items_in_basket.each do |item| %>
Day: <%= item.created_at.day %>
Month: <%= item.created_at.month %>
Year: <%= item.created_at.year %>
<% end %>
This approach will be much more reliable than parsing the string format returned by to_s.
Try converting the date to string first before using the string method split (http://ruby-doc.org/core-2.0/String.html#method-i-split).
<% splliter = item.created_at.to_s.split(" ") %>
The reason you are getting that error is because Time (http://www.ruby-doc.org/core-2.0/Time.html) class does not have split method.
I have my movies page and I'm trying to make the movie status automatically change depending on the release_date
I have this so far, but it doesnt seem to work
<% if #movie.release_date.strftime("%d %B %Y") > Time.now.strftime("%d %B %Y") %>
Released
<% else if #movie.release_date.strftime("%d %B %Y") < Time.now.strftime("%d %B %Y") %>
Upcoming
<% end %>
<% end %>
How can i make it so that if the release date I put in the form (day month year) is before the current date (day month year), it puts "Released" and then vice versa for "Upcoming" so if the release date is after the current date (day month year)
<% if #movie.release_date.past? %>
Released
<% else %>
Upcoming
<% end %>
Don't use string for comparison, compare the original object.