Is there a function I can use to show the current year in a view? I have tried
<%= Time.now %>
to try and show the time but this does not work for me.
To get an only current year, use the JavaScript getFullYear() method. JavaScript date getFullYear() method returns the year of the specified date according to local time. The value returned by getFullYear() is an absolute number.
One way is to place the current year, obtained using new Date(). getFullYear() , in a " <span> " or " <div> " element, and then inserting that SPAN or DIV into the web page.
Use the getFullYear() method to get the current year for copyright, e.g. new Date(). getFullYear() . The getFullYear method returns the current year when called on the current date.
<%= Time.current.year %>
http://pleac.sourceforge.net/pleac_ruby/datesandtimes.html
I'd rather use Date class to get year than Time class (if you only need the Date, you don't need to consider hours, minutes and seconds).
<%= Date.today.year %>
c.f. http://ruby-doc.org/stdlib-2.1.0/libdoc/date/rdoc/Date.html#method-c-today
I think the best way to get the current year considering application timezone is:
Date.current.year
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With