继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

js定时器实时获取当前时间

柠檬清煜
关注TA
已关注
手记 4
粉丝 13
获赞 224

实时获取当前时间,加个定时器,就可以按需索取,获取时间了!

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>获取当前时间</title>
    </head>
    <body>
        <div class="time"></div>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
        <script type="text/javascript">
            $(function(){
                timeNow();
                function timeNow(){
                    var date = new Date();
                    var seperator1 = "-";
                    var seperator2 = ":";
                    var month = date.getMonth() + 1;
                    var strDate = date.getDate();
                    var hour = date.getHours();
                    var minu =date.getMinutes();
                    var sec =date.getSeconds();
                    if (month >= 1 && month <= 9) {
                        month = "0" + month;
                    }
                    if (strDate >= 0 && strDate <= 9) {
                        strDate = "0" + strDate;
                    }
                    if (minu >= 0 && minu <= 9) {
                        minu = "0" + minu;
                    }
                    if (sec >= 0 && sec <= 9) {
                        sec = "0" + sec;
                    }
                    if (hour >= 0 && hour <= 9) {
                        hour = "0" + hour;
                    }
                    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + hour + seperator2 + minu + seperator2 + sec;
                    $('.time').html('年月日:'+date.getFullYear() + seperator1 + month + seperator1 + strDate +'时分秒:'+hour + seperator2 + minu +':'+sec );
                };
                var time = null;
                time=setInterval(timeNow,1000);
            });
        </script>
    </body>
</html>
打开App,阅读手记
29人推荐
发表评论
随时随地看视频慕课网APP

热门评论

楼主好用心,向楼主学习。

查看全部评论