The following function will pad number with leading zero(es). function pad(number, length) { var str = '' + number; while (str.length < length) {