JS (Date 时间对象)

//时间对象Date

        //月份单词:

        //      January:一月   February:二月      

        //      March:三月     April:四月

        //      May:五月       June:六月    

        //      July:七月      August:八月  

        //      September:九月    October:十月    

        //      November:十一月      December:十二月

//创建时间对象

        //1、创建当前时间对象

        //  创建当前这一时刻的时间对象

        var oDate =   new Date();

        console.log(oDate);// Thu Sep 01 2022 09:57:06 GMT+0800 (中国标准时间)

        //  打印输出每秒的时间

        setInterval(function(){

            var oDate1 = new Date();

            console.log(oDate1,"现在时刻");

        },1000) //每秒输出一次现在的时间

       

        //2、创建自定义时间

        //   方法一:new Date(年,月-1,日,时,分,秒) 月-1

        var a = new Date(2022,10,10,12,10,10);

        console.log(a);//Thu Nov 10 2022 12:10:00

        //   方法二:new Date("年,月,日,时:分:秒")加引号 月不用-1

        var b = new Date("2022,11,10,12:10:10");

        console.log(b);//Thu Nov 10 2022 12:10:10

        //   方法三:new Date("Nov 10 2022")加引号

        var c = new Date("Nov 10 2022");

        console.log(c);//Thu Nov 10 2022 00:00:00

       

        //日期间隔符号:

        //             2022-11-10-12:10:10

        //             2022/11/10/12:10:10

        //             2022 11 10 12:10:10

//创建时间:

        var oDate = new Date("2022,11,10,12:10:10");

        //获取时间

        //1、获取特定格式:

        //   获取年月日:

            var a = oDate.toDateString();

            console.log(a);//Thu Nov 10 2022

            var a1= oDate.toLocaleDateString();

            console.log(a1);//2022/11/10

        //   获取时分秒:

            var b = oDate.toTimeString()

            console.log(b);//12:10:10 GMT+0800 (中国标准时间)

            var b1= oDate.toLocaleTimeString();

            console.log(b1);//12:10:10

        //   获取单个时间:(获取的时number类型)

            var c1 = oDate.getFullYear()//年

            console.log(c1);//2022

            var c2 = oDate.getMonth()//月

            console.log(c2);//10 月-1

            var c3 = oDate.getDate()//日

            console.log(c3);//10

            var c4 = oDate.getHours()//时

            console.log(c4);//12

            var c5 = oDate.getMinutes()//分

            console.log(c5);//10

            var c6 = oDate.getSeconds()//秒

            console.log(c6);//10

            var c7 = oDate.getDay()//星期

            console.log(c7);//4

            var c8 = oDate.getTime()//时间戳

            console.log(c8);//1668053410000 单位时毫秒

            //时间戳

            var nDate = new Date();//获取现在的时间

            var jDate = nDate.getTime();//从1970年1月1日到现在时刻的毫秒

//倒计时:

<div></div>

    <script>

        var oDiv = document.getElementsByTagName("div")[0];

        //3、提前调用timer函数(解决1秒空白期)

        timer();

        //2、给时间戳添加间歇定时器

        setInterval(timer,1000);

        function timer(){

        // 1、获取时间戳

        //倒计时(未来时间戳-现在时间戳)

        //      

        //  1天=86400s

        //  1时=3600s

        //  1分=60s

        //获取现在时间:

        var currentDate = new Date();

        //获取未来时间:(2022年11月10日)

        var futureDate = new Date("2022,11,10");

        //获取未来时间戳-现在时间戳  将时间戳转为秒 并取整(舍去少于一秒的数)

        var time = parseInt((futureDate.getTime() - currentDate.getTime())/1000);//1584372719ms

        console.log(time);

        //将秒转为天、时、分、秒。

        var d = parseInt(time/86400);//天:time里有多少天(86400s)

        var h = parseInt((time%86400)/3600);//时:天剩下的数里有多少个时(3600s)

        var m = parseInt((time%86400%3600)/60);//分:时剩下的有多少个分(60s)

        var s = time%60;//秒:time里剩余几秒

        console.log(d,h,m,s);

        //将时、天、分、秒给要输出的内容(并添加补0操作的函数)

        oDiv.innerHTML="距离郭坨坨的生日还有"+pullZero(d)+"天"+pullZero(h)+"时"+pullZero(m)+"分"+pullZero(s)+"秒"

        }

        //4、补0操作

        function pullZero(n){

            //n大于10 执行数字前补零 不满足就执行原来的数字

            return n<10 ? "0"+n :n

        }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值