Skip to content

Instantly share code, notes, and snippets.

@TracyGJG
Created February 28, 2023 17:25
Show Gist options
  • Save TracyGJG/81a1919084ad1b9d86b1b0c01d9f9ec9 to your computer and use it in GitHub Desktop.
Save TracyGJG/81a1919084ad1b9d86b1b0c01d9f9ec9 to your computer and use it in GitHub Desktop.
Date-Time presentation formatter
function presentDateTime(dateObjectOrString) {
const pDateTime = (dateObjectOrString instanceof Date) ? dateObjectOrString : new Date(dateObjectOrString);
const pYear = nonZeroSuppression(pDateTime.getFullYear(), 4);
const pMonth = nonZeroSuppression(pDateTime.getMonth() + 1);
const pDay = nonZeroSuppression(pDateTime.getDate());
const pHours = nonZeroSuppression(pDateTime.getHours());
const pMinutes = nonZeroSuppression(pDateTime.getMinutes());
return `${pYear}-${pMonth}-${pDay}, ${pHours}:${pMinutes}`;
function nonZeroSuppression(num, width = 2) {
return `${num}`.padStart(width, '0');
}
}
const newYear = '2023-01-02T01:02:03.000Z';
const midYear = '2023-05-04T01:02:03.000Z';
console.log(presentDateTime(newYear));
console.log(presentDateTime(new Date(newYear)));
console.log(presentDateTime(midYear));
console.log(presentDateTime(new Date(midYear)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment