JS基础-特效篇(定时器)03-Date常用方法掌握

1.var cDate = new Date(); console.log(cDate);
输出:ri May 07 2021 15:00:45 GMT+0800 (中国标准时间)
2.常用方法

<script>
    var cDate = new Date();
    console.log(cDate);
    //Fri May 07 2021 15:17:25 GMT+0800 (中国标准时间)
    console.log(cDate.getTime());
    //1620371845943  返回毫秒数
    console.log(cDate.getMilliseconds());
    //943  ?
    console.log(cDate.getSeconds());
    //25  
    console.log(cDate.getMinutes());
    //17
    console.log(cDate.getHours());
    //15
    console.log(cDate.getDay());
    //5  返回星期,0周日 6周六
    console.log(cDate.getDate());
    //7  返回当前月的第几天
    console.log(cDate.getMonth());
    //4  返回月份,从0开始
    console.log(cDate.getFullYear());
    //2021
</script>

3.写一个函数,格式化日期对象:返回YYYY-MM-dd hh:mm:ss

<script>
    var c = formatDate(new Date());
    console.log(c);

    function formatDate(d) {  
        /**
         * @Description: 格式化函数
         * @author Tsukiis Chen
         * @date 2021/5/7
         * @param {Date}date
         */
        //验证
        if (!d instanceof Date) {
            return;
        }
        //    转化
        var year = d.getFullYear();
        var mouth = d.getMonth() + 1;
        var day = d.getDate();
        var hour = d.getHours();
        var minute = d.getMinutes();
        var second = d.getSeconds();
        /*转换格式*/
        mouth = mouth < 10 ? '0' + mouth : mouth;
        day = day < 10 ? '0' + day : day;
        hour = hour < 10 ? '0' + hour : hour;
        minute = minute < 10 ? '0' + minute : minute;
        second = second < 10 ? '0' + second : second;

        return year + '-' + mouth + '-' + day + ' ' + hour + ':' + minute + ':'+second;

    }
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值