时间搓到格式化日期转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/smalCat/article/details/81707657
// 用于时间格式化,针对时间搓转化
export function formatDate (date, str) {
  if (/(y+)/.test(str)) {
    str = str.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(str)) {
      let stry = o[k] + '';
      str = str.replace(RegExp.$1, (RegExp.$1.length === 1) ? stry : padLeftZero(stry));
    }
  }
  return str;
};
function padLeftZero (str) {
  return ('00' + str).substr(str.length);
}

猜你喜欢

转载自blog.csdn.net/smalCat/article/details/81707657