Skip to content

Instantly share code, notes, and snippets.

@Joaquim3
Created August 3, 2024 10:26
Show Gist options
  • Select an option

  • Save Joaquim3/118ab3acd9020be2e7cd74e63a62c769 to your computer and use it in GitHub Desktop.

Select an option

Save Joaquim3/118ab3acd9020be2e7cd74e63a62c769 to your computer and use it in GitHub Desktop.
JAVASCRIPT ⇢ retrieve a date with a specific format
//----------------------------------------------------------
// purpose : retreive a date with a specific format
// example = 31-01-2024 01:27:55
//----------------------------------------------------------
function fncFormatDate() {
let currentDatetime = new Date();
let formattedDate = fncaddLeadingZeros(currentDatetime.getDate()) + "-" +
fncaddLeadingZeros(currentDatetime.getMonth() + 1) + "-" +
currentDatetime.getFullYear() + " " +
fncaddLeadingZeros(currentDatetime.getHours()) + ":" +
fncaddLeadingZeros(currentDatetime.getMinutes()) + ":" +
fncaddLeadingZeros(currentDatetime.getSeconds());
return formattedDate;
}
//------------------------------
function fncaddLeadingZeros(n) {
if (n <= 9) {
return "0" + n;
}
return n;
}
//example
fncFormatDate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment