JS基础语法---Date对象---格式化日期

格式化后的指定格式的日期和时间,封装一个函数

    function getDate() {
      var dt = new Date();
      var year = dt.getFullYear();
      var month = dt.getMonth();
      var date = dt.getDate();
      var hour = dt.getHours();
      var minute = dt.getMinutes();
      var second = dt.getSeconds();

      month = month < 10 ? "0" + month : month;
      date = date < 10 ? "0" + date : date;
      hour = hour < 10 ? "0" + hour : hour;
      minute = minute < 10 ? "0" + hour : hour;
      second = second < 10 ? "0" + second : second;

      return year + "年" + month + "月" + date + "日" + hour + ":" + minute + ":" + second;
    }

    console.log(getDate(new Date()));

 

posted @ 2019-12-03 11:15  jane_panyiyun  阅读(708)  评论(0编辑  收藏  举报