JavaScript (时间格式化函数)

        // 时间格式化函数
            function timeFormat() {
                var time = new Date()
                var year = time.getFullYear()
                var month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
                var day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate()
                var hour = time.getHours() < 10 ? '0' + time.getHours() : time.getHours()
                var minute = time.getMinutes() <10 ? '0'+ time.getMinutes() : time.getMinutes()
                var second = time.getSeconds() <10 ? '0'+ time.getSeconds() : time.getSeconds()
                let currentTime = `${year} 年 ${month} 月 ${day} 日 ${hour} 时 ${minute} 分 ${second} 秒`
                return currentTime
            }
setInterval(function () {
      //定时器,一秒钟执行一次 console.log(timeFormat()) },
1000)
原文地址:https://www.cnblogs.com/lxsunny/p/13665184.html