shorter time code that achieves same results and respects locale's AM-PM preference

master
Aevann 2023-09-26 13:20:45 +03:00
parent d0ef9768ed
commit f8c177172d
2 changed files with 3 additions and 12 deletions

View File

@ -1,8 +1,5 @@
function formatDate(d) { function formatDate(d) {
const hour = ("0" + d.getHours()).slice(-2); return d.toLocaleTimeString()
const minute = ("0" + d.getMinutes()).slice(-2);
const second = ("0" + d.getSeconds()).slice(-2);
return hour + ":" + minute + ":" + second;
} }
for (const e of timestamps) { for (const e of timestamps) {

View File

@ -231,14 +231,8 @@ function showmore(t) {
} }
function formatDate(d) { function formatDate(d) {
const year = d.getFullYear(); const options = {year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', timeZoneName: 'short'};
const monthAbbr = d.toLocaleDateString('en-us', {month: 'short'}); return d.toLocaleTimeString([], options)
const day = d.getDate();
const hour = ("0" + d.getHours()).slice(-2);
const minute = ("0" + d.getMinutes()).slice(-2);
const second = ("0" + d.getSeconds()).slice(-2);
const tzAbbr = d.toLocaleTimeString('en-us', {timeZoneName: 'short'}).split(' ')[2];
return day + " " + monthAbbr + " " + year + " " + hour + ":" + minute + ":" + second + " " + tzAbbr;
} }
const timestamps = document.querySelectorAll('[data-time]'); const timestamps = document.querySelectorAll('[data-time]');