Created
July 14, 2015 23:28
-
-
Save jeremywadsack/307f576999dd2b1b2f0d to your computer and use it in GitHub Desktop.
Covert UTC time to local time on browser
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
# A jQuery plugin to convert UTC times to local times on the browser. | |
# | |
# You can apply this to any element on the page like this: | |
# | |
# $('#message').covertToLocalDate(); | |
# | |
# This looks for one or more date-time strings in the original element | |
# and replaces them. The existing dates must be in the format | |
# 2015-07-14 16:30:00 UTC | |
# It replaces them with an <abbr> element (which is usually styled with | |
# a dotted underscore and the question-mark cursor to hint that there is | |
# more details on hover). The element has a title with the original text | |
# but the text of the element is converted to local time in default format: | |
# Tue Jul 14 2015 09:30:00 GMT-0700 (PDT) | |
( ($) -> | |
$.fn.covertToLocalDate = -> | |
this.each -> | |
$this = $(this) | |
text = this.outerHTML | |
newText = text.replace /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} UTC/g, (match)-> | |
localDate = new Date(match).toString() | |
"<abbr title=\"#{match}\">#{localDate}</abbr>" | |
$this.replaceWith newText | |
this | |
)(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment