Skip to main content

Dynamic Footer Timestamp in Rails

Chris Oliver December 31, 2019

Ever seen the typical copyright message at the bottom of a website that's out of date? Yep. A surprising amount of websites have them!

© 1992 Your Company

Someone just hard coded the year and called it a day.

That's fine and dandy and it's not hard to change, but it's easy to forget you need to update it.

A better solution? Use Rails to dynamically generate it.

© <%= Date.current.year %> Your Company

This will always print the current year in your footer. We use Date.current because we're using Rails and this method will take into account the current time zone in Rails.

You can also use Date.today.year to use pure Ruby, but this won't apply the current time zone to the calculation.

Either way, this is will display the current year from the server side and will always be accurate.

An alternative is to use client side Javascript for this, but it will instead use the browser's timezone so it will display the year in their time zone.

© <script>new Date().getFullYear()>2010&&document.write("-"+new Date().getFullYear());</script> Your Company

P.S. You might enjoy following me on Twitter.


Loading...

Latest from the blog

Start building fast, modern Rails websites in no time with GoRails tutorials.

Subscribe to the newsletter

Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.