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) {
const hour = ("0" + d.getHours()).slice(-2);
const minute = ("0" + d.getMinutes()).slice(-2);
const second = ("0" + d.getSeconds()).slice(-2);
return hour + ":" + minute + ":" + second;
return d.toLocaleTimeString()
}
for (const e of timestamps) {

View File

@ -231,14 +231,8 @@ function showmore(t) {
}
function formatDate(d) {
const year = d.getFullYear();
const monthAbbr = d.toLocaleDateString('en-us', {month: 'short'});
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 options = {year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', timeZoneName: 'short'};
return d.toLocaleTimeString([], options)
}
const timestamps = document.querySelectorAll('[data-time]');