Last active
December 19, 2022 06:20
-
-
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.
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
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