获取时当前和两天时间案例(格式:2020-07-02 15:01:41)

119 阅读1分钟

获取当前时间 格式 (2020-07-02 15:01:41)

export const currentTime = () => {
  const now = new Date();
  const year = now.getFullYear(); // 年
  const month = now.getMonth() + 1; // 月
  const day = now.getDate(); // 日
  const hh = now.getHours(); // 时
  const mm = now.getMinutes(); // 分
  const ss = now.getSeconds(); // 秒
  let clock = `${year}-`;
  if (month < 10) clock += '0';
  clock += `${month}-`;
  if (day < 10) clock += '0';
  clock += `${day} `;
  if (hh < 10) clock += '0';
  clock += `${hh}:`;
  if (mm < 10) clock += '0';
  clock += `${mm}:`;
  if (ss < 10) clock += '0';
  clock += ss;
  return clock;
};

两天前时间

  export const twoDaysAgoTime = () => {
  const now = new Date();
  const year = now.getFullYear(); // 年
  const month = now.getMonth() + 1; // 月
  const day = now.getDate(); // 日
  const hh = now.getHours(); // 时
  const mm = now.getMinutes(); // 分
  const ss = now.getSeconds(); // 秒
  let clock = `${year}-`;
  if (month < 10) clock += '0';
  clock += `${month}-`;
  if (day < 10) clock += '0';
  clock += `${`${day}` - 1} `;
  if (hh < 10) clock += '0';
  clock += `${hh}:`;
  if (mm < 10) clock += '0';
  clock += `${mm}:`;
  if (ss < 10) clock += '0';
  clock += ss;
  return clock;
};