js显示当前日期时间和星期几,上午和下午

function showTime() {
  let show_day = new Array(
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",
    "星期日"
  );
  let time = new Date();
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let date = time.getDate();
  let day = time.getDay();
  let hour = time.getHours();
  let minutes = time.getMinutes();
  let second = time.getSeconds();
  month < 10 ? (month = "0" + month) : month;
  // month = month + 1;
  hour < 10 ? (hour = "0" + hour) : hour;
  minutes < 10 ? (minutes = "0" + minutes) : minutes;
  second < 10 ? (second = "0" + second) : second;
  let now_time =
    // "当前时间:" +
    year +
    "年" +
    month +
    "月" +
    date +
    "日" +
    " " +
    show_day[day - 1] +
    " " +
    hour +
    ":" +
    minutes +
    ":" +
    second;
  return now_time
};

 

export function showTime() {
  let show_day = new Array(
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",
    "星期日"
  );
  let time = new Date();
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let date = time.getDate();
  let day = time.getDay();
  let hour = time.getHours();
  let minutes = time.getMinutes();
  let second = time.getSeconds();
  month < 10 ? (month = "0" + month) : month;
  // month = month + 1;
  console.log('hour', hour)
  // hour < 10 ? (hour = "0" + hour) : hour;
  let hourtext = hour >= 12 ? "下午" : "上午";
  hour >= 12 ? (hour = hour - 12) : (hour);
  minutes < 10 ? (minutes = "0" + minutes) : minutes;
  second < 10 ? (second = "0" + second) : second;
  let now_time =
    // "当前时间:" +
    year +
    "年" +
    month +
    "月" +
    date +
    "日" +
    " " +
    // show_day[day - 1] +
    " " +
    hourtext +
    hour +
    ":" +
    minutes +
    ":" +
    second;
  return now_time
};

 

posted @ 2021-06-24 16:30  abcByme  阅读(1489)  评论(0编辑  收藏  举报