Created
August 3, 2024 10:26
-
-
Save Joaquim3/118ab3acd9020be2e7cd74e63a62c769 to your computer and use it in GitHub Desktop.
JAVASCRIPT ⇢ retrieve a date with a specific format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //---------------------------------------------------------- | |
| // 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