Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Last active February 7, 2020 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save niksumeiko/6856869 to your computer and use it in GitHub Desktop.
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).
/**
* 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') );
@niksumeiko
Copy link
Author

Google Closure Library has a great function goog.string.padNumber that extends this gist function with number decimal precision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment