Sterling Rose Design Blog

Perpetual Footer

2 Comments
Tags: date and time Rails

I was spinning up a brand new client website that would (like most websites) include a copyright footer. I wanted to make sure I wouldn’t have to come back year after year and update the year span, so here is what I used:

&copy; 2009<%= "-#{Date.today.year}" if Date.today.year != 2009 %> ABC Company


It will display only 2009 as the copyright year until January 1, 2010, then it will start displaying a span (from 2009 through the current year). Simple.

Uses Rails 2.3.2

Rendering datetime in a user's timezone

0 Comments
Tags: Rails date and time

We recently had a client who wanted to account for a user’s time zone when they entered dates for various fields. For example, if a user were to input a new bill, the bill entry date should default to the current date. Time.now is what we had been using, but that would only give the right date if you were on the correct side of the magic UTC dividing line.

The solution to this problem involved 1) storing a user’s time zone in a string field in the User model, and 2) adjusting Time.now for that time zone.

For part 1, we used Rails’ built-in time_zone_select method:

<%= form.label :time_zone, "Time Zone" %>
<%= time_zone_select :time_zone, @user.time_zone, nil, :default => 
  "Pacific Time (US & Canada)" %>


For part 2, the solution was to add a method to the User model to get the user’s current date:
Read the whole post...


Copyright 2007-2010, Sterling Rose Design. All rights reserved.