js 设置固定位数。时间月日小时分钟秒

function fix(num, length) {
return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
}

fix(1234, 8);
// "00001234"
fix(1234, 2);
// "1234"

posted @ 2021-08-21 15:07  郑州谷多软件  阅读(152)  评论(0编辑  收藏  举报