Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show current year in view?

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.

like image 316
Zakoff Avatar asked May 30 '11 10:05

Zakoff


People also ask

How do I get current year only?

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.

How to display the current year in html?

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.

How do I get the current year in footer?

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.


3 Answers

<%= Time.current.year %>

http://pleac.sourceforge.net/pleac_ruby/datesandtimes.html

like image 107
Emil Ahlbäck Avatar answered Oct 19 '22 21:10

Emil Ahlbäck


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

like image 49
Naoyoshi Aikawa Avatar answered Oct 19 '22 19:10

Naoyoshi Aikawa


I think the best way to get the current year considering application timezone is:

Date.current.year
like image 47
freemanoid Avatar answered Oct 19 '22 19:10

freemanoid