Skip to content

Instantly share code, notes, and snippets.

@1000heads-luke
Last active December 19, 2022 06:20
Show Gist options
  • Save 1000heads-luke/8d4527f16bd11b9f1d6e541f98971f5a to your computer and use it in GitHub Desktop.
Save 1000heads-luke/8d4527f16bd11b9f1d6e541f98971f5a to your computer and use it in GitHub Desktop.
jQuery function which identifies all HTML "<time>" elements with a DATETIME attribute. It then replaces the contents of those elements with the Localised DateTime (based on the viewer's language and clock settings) and moves the original content to the TITLE attribute.
jQuery(function($){
$('time[datetime]').each(function(k,v){
const $t = $(this);
const dt = $t.attr('datetime');
const d = new Date(Date.parse(dt));
if( !d )
return;
const s = d.toLocaleString(
window.navigator.language ,
{
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric'
}
);
if( !s || s == 'Invalid Date' )
return;
$t
.attr('title',$t.text())
.text(s);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment