Blog / Dynamic Footer Timestamp in Rails

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.


Comments
Loading...

Want to stay up-to-date with Ruby on Rails?

Join 38,558+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.