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'
Related
I have a site that uses ruby (rhtml) files which I am not used to editing (more used to php). I only have access to these files to edit (so not the back-end).
In the database I have a date that I can display using:
<%= zp.date %>
which will display e.g. 14/10/2010
I can display today's date using:
<%= Date.today.strftime("%Y%m%d") %>
which will display 20160122.
What I'm trying to work out is how I can compare the dates and echo the difference and in turn display something different.
e.g.
compare <%= zp.date %> and <%= Date.today.strftime("%Y%m%d") %>.
If days difference is less than 20 echo XXX
else
If days difference between 20 - 40 echo XXX
else
If days difference between 41 - 60 echo XXX
Any help would be appreciated (links etc), code examples would be greatly appreciated.
Again to clarify I can only edit the rhtml.
Dates are directly comparable with if-statements, and computing the difference will show you the number of days between them. Assuming zp.date is in the past:
<% date_difference = Date.today - zp.date %>
<% if date_difference < 20 %>
...
<% elsif date_difference <= 40 %>
...
<% elsif date_difference <= 60 %>
...
<% else %>
Difference is greater than 60
<% end %>
Note the <% %> tags, which are used for if-logic and internal Ruby code - basically any expression that shouldn't be output (thanks #QPaysTaxes), instead of <%= %> which is the output tag.
If you don't know whether zp.date is in the past or future, you can use the abs method on the result:
<% date_difference = (Date.today - zp.date).abs %>
And if zp.date isn't actually a Date object, then parse it:
<% date_difference = (Date.today - Date.parse(zp.date)).abs %>
Older Ruby versions will need strptime when the date string is ambiguous:
<% date_difference = (Date.today - Date.strptime(zp.date, '%d/%m/%Y')).abs %>
<% current_user.events.find(:all, conditions: ("start" > DateTime.now )).limit(5).each do |e| %>
<%= e.start %>
<% end %>
I get this error msg:
invalid date
this does not work because the format is not correct.
I can use e.start.to_date then I can compare the e.event with TimeDate.now but I don't know how to map the value to a DateTime.
If I do it like this:
<% current_user.events.map! { |e| e.start.to_date }.limit(5).each do |e| %>
<%= e.start %>
<% end %>
It's an array and therefore the good limit, where, select Active Record methods won't work anymore.
Basically I want to map all the start values to date then I want to compare them and show only today's events or future events but not from the past.
How would you solve this?
Can you try:
current_user.events.where("start > ?", Time.now.to_date)
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'm using similar code to Railscast 213 to display a calendar with records.
The do line is causing a "getting wrong number of arguments (1 for 0):
<%= calendar #date do |date| %>
<%= date.day %>
<% if #wolabors_by_date[date] %>
<ul>
<% #wolabors_by_date[date].each do |wolabor| %>
<li><%= link_to wolabor.name, wolabor %></li>
<% end %>
</ul>
<% end %>
<% end %>
The calendar_helper.rb starts out with:
module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, block).table
end
wolabors_controller.rb has
class WolaborsController < ApplicationController
def index
#wolabors = Wolabor.all
#wolabors_by_date = #wolabors.group_by(&:date)
#date = params[:date] ? Date.parse(params[:date]) : Date.today
end`
I think that first line is supposed to be
<% calendar_for #date do |date| %>
That railscast has been revised and it does not use that table_builder plugin in the new version.
http://railscasts.com/episodes/213-calendars-revised
I've found in the discussion about this Railscast , that the statement :
first = date.beginning_of_month.beginning_of_week(START_DAY)
gives the same arguments error . It seems methods
beginning_of_month
and
beginning_of_week
are Rails 3.2 specific and if you are on lower version , you should upgrade.
In my app I can add offers and give them a start and end date, the offers are displayed as partials. How can I go about only displaying them if the start date is todays date or earlier and the end date has not yet been reached?
So far I have:
offer partial
<% if offer.date_from > Date.today && offer.date_to < Date.today %>
<div class="offer_partial">
<div class="offer_title">
<%= offer.title %>
</div>
<div class="offer_body">
<%= offer.body %>
</div>
</div>
<% end %>
But this gives a undefined method `>' for nil:NilClass error.
Is it even ok to do these kind of checks in the view?
Thanks very much for any help!
To answer your second question first, no, you should build a collection of the Offer models through a query in the controller, thereby leaving the view to just iterate through that collection and render the HTML.
Also, the if block doesn't do what you want it to. You said "Today's date or earlier", but you're checking for today's date or later. It seems that you've inverted the logic.
The error you're getting means that date_from is nil. Are you validating those fields or are they allowed to have a nil value?
This is how I would set that collection up:
class Offer < AR::Base
scope :end_date_not_reached, where("date_from <= ?", Date.today).where("date_to > ?", Date.today)
end
In the controller:
#offers_in_progress = Offer.end_date_not_reached
In the view:
<% #offers_in_progress.each do |offer| %>
<!-- stuff here -->
<% end %>
Hope this helps.