JS 时间格式转换
2019-07-13 本文已影响0人
Spidd
> 银行卡格式转换
```
// 每隔4个字符加空格间隔
const replaceStr = str => str.replace(/(\d{4})(?=\d)/g, '$1 ');
```
```
传入的参数格式为 yyyy-MM-dd hh:mm:ss 用正则替换后仅显示天数dd 代码如下
let s ="2019-07-13T15:58:53"; //yyyyMMddHHmmss
let reg =/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/;
s = s.replace(reg, "$1年$2月$3日"); // yyyy/MM/dd HH:mm:ss
console.log(s) //2019年07月13日
{
let str ='2018-03-20';
let reg =/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
let {year, month, day} = str.match(reg).groups;
console.log(year,month,day)//2018 03 20
}
{
let reg =/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
let str ='2018-03-20';
str.replace(reg, (...args)=>{
console.log(...args)
let {year, month, day} = args[args.length-1];
console.log(year, month, day)
})
}
```