Last active
February 7, 2020 18:29
-
-
Save niksumeiko/6856869 to your computer and use it in GitHub Desktop.
JavaScript function that converts a number into the 2 digits day of the month with leading zeros (01-31). Very useful when working with the output that holds days of the month without leading zeros (1-31).
This file contains 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
/** | |
* Turns a number/string to 2 digits day of the month with leading zero. | |
* @param {number|string} day to turn into day. | |
* @return {string} | |
*/ | |
function numberToDay(j) { | |
return ('0' + j).slice(-2); | |
} | |
// Examples: | |
// Turning different type days into 2 digits number. | |
console.log( numberToDay(7) ); | |
console.log( numberToDay('21') ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Google Closure Library has a great function
goog.string.padNumber
that extends this gist function with number decimal precision.