Skip to content

Commit

Permalink
update dateTime so that it works correctly for all locales
Browse files Browse the repository at this point in the history
including those that don't zero-pad their hours or use 24-hour time
The old approach here, which uses substr to cut off the
toLocaleTimeString() output at 5 characters does not work
for locales with formatting
    9:20:35 PM
It incorrectly grabs the first 5 chars including the trailing colon
    9:20:
The new approach here will instead return
    09:20 PM
for these locales while still producing sensible output
for 24-hour locales
  • Loading branch information
dansvo committed Sep 15, 2020
1 parent e6f0bcd commit 072ae04
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/IHP/static/helpers.js
Expand Up @@ -32,7 +32,7 @@ function initTime() {

document.querySelectorAll(".date-time").forEach(function(elem){
var date = new Date(elem.dateTime);
elem.innerHTML = date.toLocaleDateString() +", " + date.toLocaleTimeString().substr(0,5);
elem.innerHTML = date.toLocaleDateString() +", " + date.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
});

document.querySelectorAll(".date").forEach(function(elem){
Expand Down

0 comments on commit 072ae04

Please sign in to comment.