时间戳转换两种事件类型

2020-04-19  本文已影响0人  张思学
function timeFunc(content, type) {
  const date = new Date(content);
  const y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  let d = date.getDate();
  d = d < 10 ? '0' + d : d;
  let h = date.getHours();
  h = h < 10 ? '0' + h : h;
  const minute = date.getMinutes();
  const second = date.getSeconds();
  if (type === 'date') {
    return y + '-' + m + '-' + d;
  }
  if (type === 'time') {
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  }
}

timeFunc('1587295219000', 'date'); // 日期
timeFunc('1587295219000', 'time'); // 日期+时间
上一篇 下一篇

猜你喜欢

热点阅读