获取时间、倒计时

获取时间

<body>
    <p id="count"></p>
    <script>
        var count = document.getElementById('count')
        function time(){
            var date = new Date();
            console.log(date)
            var arr = ['周日','周一','周二','周三','周四','周五','周六',]
            var week = date.getDay()//从周日~周六 0~6
            console.log(arr[week])
            var year = date.getFullYear()
            console.log(year)
            var month = date.getMonth()+1
            console.log(month)//月份 0~11 计数
            var day = date.getDate()
            console.log(day)
            var hours = date.getHours()
            console.log(hours)
            var mins = date.getMinutes()
            console.log(mins)
            var sec = date.getSeconds()
            console.log(sec)
            var i = date.getMilliseconds()//毫秒
            console.log(i)
            console.log(date.getTime())//时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
            
            count.innerHTML = '当前时间:'+year+'年'+bl(month)+'月'+bl(day)+'日'+arr[week]+bl(hours)+'时'+bl(mins)+'分'+bl(sec)+'秒'
        
        }
        time()
        console.log(time())
        var timer = setInterval(function(){
            time()
            
        }, 1000);
        function bl(x){
            return x<10?'0'+x:x
        }
    </script>
</body>

倒计时

<body>
    <p id="count"></p>
    <script>
        var count = document.querySelector('#count')
        
        function djs(a,b,c,d,e,f,g){
            var startDate = new Date()
            // var endDate = new Date(2019,8,15,12,0,0)
            var endDate = new Date(a,b-1,c,d,e,f)
            var time = parseInt((endDate - startDate)/1000)
            console.log(time)
            var d = parseInt(time/60/60/24)
            var h = parseInt(time/60/60%24)
            var m = parseInt(time/60%60)
            var s = parseInt(time%60)
            count.innerHTML = g+d+'天'+h+'时'+m+'分'+s+'秒'
                
            
        }
        // djs()
        djs(2019,08,15,12,0,0,'距离还有:')
        var timer = setInterval(function(){
            // djs()
            djs(2019,08,15,12,0,0,'距离还有:')
        },1000)
    setInterval(djs,1000,'2020,1,1 00:00:00','距离元旦:')
        
        
    </script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值