Skip to content

Instantly share code, notes, and snippets.

@davidlee
Created October 20, 2009 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidlee/214687 to your computer and use it in GitHub Desktop.
Save davidlee/214687 to your computer and use it in GitHub Desktop.
Pure javascript timezone management for Rails
module ApplicationHelper
def local_time(time)
capture_haml do
haml_tag :span, time.utc.to_s, :class => 'time'
end
end
def local_date(time)
capture_haml do
haml_tag :span, time.to_time.utc.to_s, :class => 'date'
end
end
end
// global.js
$('span.time').each(function(){
utc_time = new Date($(this).html());
$(this).replaceWith(utc_time.toLocaleTimeString());
});
$('span.date').each(function(){
utc_time = new Date($(this).html());
$(this).replaceWith(utc_time.toLocaleDateString());
});
# example usage (haml)
%td.created
= local_date(ticket.created_at)
= local_time(ticket.created_at)
# example output (generated html)
<td class='created'>
<span class='date'>Tue Oct 20 22:17:14 UTC 2009</span>
<span class='time'>Tue Oct 20 22:17:14 UTC 2009</span>
</td>
# example output (rendered)
<td class="created">
10/21/2009
09:17:14
</td>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment