0埋め padStart padEnd slice
文字列.padStart(長さ,[追加文字列]) // 追加文字列は省略可
console.log('10'.padStart(5, '*')); // ***10
console.log('12345678'.padStart(5, '*')); // 12345678
console.log('10'.padStart(5, '*-+')); // *-+10
console.log('10'.padEnd(5, '*-+')); // 10*-+
console.log('10'.padEnd(5)); // '10const fromTime = Date.now();
let nowTime;
let count = 0;
showTime();
function showTime() {
const timeoutId = setTimeout(() => {
const pastMs = new Date(Date.now() - fromTime).getMilliseconds();
const zeroPad = String(pastMs).padStart(4, '0');
console.log(zeroPad);
count++;
showTime();
}, 100);
if (count > 10) {
clearTimeout(timeoutId);
}
}const num = 1234; // 0詰めする数字
const result = ("00000" + num).slice(-5); // できた文字列
console.log(result); // 01234