js时间戳封装

2019-07-11  本文已影响0人  肆意放纵

懒人话不多,直接上代码

export const formatDate = function(now) {
  //年-月-日-时-分-秒
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let date = now.getDate();
  let hour = now.getHours();
  let minute = now.getMinutes();
  let second = now.getSeconds();
  //let arr = [month, date, hour, minute, second];
  if (month < 10) {
    month = "0" + month;
  }
  if (date < 10) {
    date = "0" + date;
  }
  if (hour < 10) {
    hour = "0" + hour;
  }
  if (minute < 10) {
    minute = "0" + minute;
  }
  if (second < 10) {
    second = "0" + second;
  }
  return (
    //按需求拼接   
    year + "-" + month + "-" + date
    // year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second
  );
};
上一篇 下一篇

猜你喜欢

热点阅读