format某个格式的日期
Opened this issue · 0 comments
lvleihere commented
将时间戳20180106000000 -> 转成 2018-01-06 00:00:00 或者其他
// xxxx-xx-xx xx:xx:xx 的形式
const formatData = (str, formater) => {
const arr = str.split('');
let i = 0;
return formater.replace(/x/g, () => arr[i++]);
};
const str = '20180106000000';
const formater = 'xxxx-xx-xx xx:xx:xx';
const formatRes = formatData(str, formater);
console.log(formatRes); // 2018-01-06 00:00:00
// YY-MM-DD hh:mm:ss 的形式
const formatData = (str, formater) => {
const arr = str.split('');
let i = 0;
return formater.replace(/[YMDhms]/g, () => arr[i++]);
};
const str = '20180106000000';
// const formater = 'xxxx-xx-xx xx:xx:xx';
const formater = 'YY-MM-DD hh:mm:ss';
const formatRes = formatData(str, formater);
console.log(formatRes); // 2018-01-06 00:00:00