Skip to content

Instantly share code, notes, and snippets.

Last active December 30, 2015 15:09
  • Star 1 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 aprice/7846212 to your computer and use it in GitHub Desktop.
This snippet uses jQuery to replace the text of elements with data-datetime, data-date, or data-time attributes containing a JavaScript (millisecond) timestamp with local & locale date/time/datetime strings. No more asking users for locale information!
/* e.g. <span data-datetime="1395760693000">2014-03-25 15:18:13</span> will be replaced with localized date/time on load */
$("[data-datetime]").each(function () {
el = $(this);
el.text(new Date(parseInt("datetime"))).toLocaleString());
/* e.g. <span data-date="1395760693000">2014-03-25</span> */
$("[data-date]").each(function () {
el = $(this);
el.text(new Date(parseInt("date"))).toLocaleDateString());
/* e.g. <span data-time="1395760693000">15:18:13</span> */
$("[data-time]").each(function () {
el = $(this);
el.text(new Date(parseInt("time"))).toLocaleTimeString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment