为了账号安全,请及时绑定邮箱和手机立即绑定
手记 \ js定时器实时获取当前时间

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

2016-12-30 16:29:27 1303浏览 2评论

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

<!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>

本文原创发布于慕课网 ,转载请注明出处,谢谢合作!

相关标签: JQuery JavaScript
23 推荐

    柠檬清煜

    Web前端工程师 态度决定高度!

    作者的热门手记

    意见反馈 常见问题 APP下载
    官方微信