-
-
Save hoyangtsai/515ee5a1e27a82b64a610a1219855e1f 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)
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
ES new feature - padStart
http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart