Skip to content

Num-to-string (padded)

ts
function NumToStrPad(num, length: number): string;

This function takes a number num and returns a string representation of that number padded with zeroes to ensure that the resulting string is not shorter than length. Useful, for example, when you want a 2-digit representation of month and day number in a date.

Example

ts
{
  var date = new Date();
  var month = date.getMonth() + 1;
  monthStr = NumToStrPad(month, 2);
  // Ex: in May, monthStr will contain the string "05"
}