首页 > 其他 > 详细

倒计时(三)之时分秒格式化

时间:2021-05-21 14:20:50      阅读:13      评论:0      收藏:0      [点我收藏+]

Date() 是一个构造函数,必须使用new来调用创建日期对象

var date = new Date();
console.log(date);      // 显示当前时间
// 没有输入任何参数,会直接返回系统设置的当前时间

日期格式化

var date = new Date();
console.log(date.getFullYear());       // 显示现在的年份
console.log(date.getMonth() + 1);          // 显示现在的月份,返回的月份小1一个月,所以要+1
console.log(date.getDate());             // 显示现在的日期
console.log(date.getDay());            // 显示星期几,周日0-周六6 但返回的是阿拉伯数字

时分秒格式化

var date = new Date();
        console.log(date.getHours());     // 显示时
        console.log(date.getMinutes());   // 显示分钟
        console.log(date.getSeconds());   // 显示秒
        // 封装一个函数返回当前时分秒
        function getTimer() {
            var time = new Date();
            var h = time.getHours();
            h = h < 10 ? ‘0‘ + h : h;    // 数字补0
            var m = time.getMinutes();
            m = m < 10 ? ‘0‘ + m : m;
            var s = time.getSeconds();
            s = s < 10 ? ‘0‘ + s : s;
            return h + ‘:‘ + m + ‘:‘ + s;
        }
        console.log(getTimer());

倒计时(三)之时分秒格式化

原文:https://www.cnblogs.com/L-hua/p/14790605.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!