创建分钟/小时数组, 个位数补0 String.prototype.padStart()

// 获取从start到end的数组, 个位数补0
export function getDateArray(start: number, end: number) {
  const arr = [];
  for (let i = start; i <= end; i++) {
    arr.push(i.toString().padStart(2, '0'));
  }
  return arr;
}
// console.log(getDateArray(0, 23);
// 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, ... 22, 23

String.prototype.padStart()

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.
padStart() 方法用另一个字符串(多次,如果需要)填充当前字符串,直到结果字符串达到给定长度。 从当前字符串的开头应用填充。

string.padStart(maxLength fillString);
maxLength: 转化的长度(数据大于maxLength, 会返回原数据)
fillString: 填充内容 (会重复多次, 直到达到maxLength的长度)


'abc'.padStart(10);         // "       abc"
'abc'.padStart(10, "foo");  // "foofoofabc"
'abc'.padStart(6,"123465"); // "123abc"
'abc'.padStart(8, "0");     // "00000abc"
'abc'.padStart(1);          // "abc"

而 String.prototype.padEnd() 是加在原字串的后面, 用法一样.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值