Last active
December 30, 2015 15:09
-
-
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!
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
/* 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