js 格式化时间

export const formatTime = (time) => {
  if (!time) {
    return '';
  }
  const y = time.substr(0, 4);
  const m = time.substr(4, 2);
  const d = time.substr(6, 2);
  return `${y}-${m}-${d}`;
};
// 格式化时间

export const getYYMMDD = () => {
  const time = new Date();
  const y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  const h = time.getHours();
  m = m < 10 ? `0${m}` : m;
  d = d < 10 ? `0${d}` : d;
  return `${y}-${m}-${d}`;
};
// 获取当前时间并格式化
export const getNowTime = () => {
  const time = new Date();
  const y = time.getFullYear();
  let m = time.getMonth() + 1;
  let d = time.getDate();
  let h = time.getHours();
  let mi = time.getMinutes();
  let s = time.getSeconds();
  m = m < 10 ? `0${m}` : m;
  d = d < 10 ? `0${d}` : d;
  h = h < 10 ? `0${h}` : h;
  mi = mi < 10 ? `0${mi}` : mi;
  s = s < 10 ? `0${s}` : s;
  return `${y}-${m}-${d} ${h}:${mi}:${s}`;
};

 

posted @ 2020-12-31 10:47  拈花醉  阅读(243)  评论(0编辑  收藏  举报