Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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(el.data("datetime"))).toLocaleString());
});
/* e.g. <span data-date="1395760693000">2014-03-25</span> */
$("[data-date]").each(function () {
el = $(this);
el.text(new Date(parseInt(el.data("date"))).toLocaleDateString());
});
/* e.g. <span data-time="1395760693000">15:18:13</span> */
$("[data-time]").each(function () {
el = $(this);
el.text(new Date(parseInt(el.data("time"))).toLocaleTimeString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment