Created
October 20, 2009 22:34
-
-
Save davidlee/214687 to your computer and use it in GitHub Desktop.
Pure javascript timezone management for Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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