Skip to content

Instantly share code, notes, and snippets.

Created July 14, 2015 23:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jeremywadsack/307f576999dd2b1b2f0d to your computer and use it in GitHub Desktop.
Covert UTC time to local time on browser
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment