Home > Web Front-end > JS Tutorial > JS gets the current time instance code

JS gets the current time instance code

小云云
Release: 2018-03-13 15:28:24
Original
2082 people have browsed it

This article mainly shares with you the JS code to obtain the current time example. We first share a picture with you, hoping to help you.

Basic properties of Date object:

Get the current time:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #box{
            width:300px;
            height:50px;
            border:1px solid black;
        }
    </style>
</head>
<body>
<p id="box"></p>
<script type="text/javascript">
    window.onload=function(){
        function checkTime(i){      //此函数用来检查获取的数值是否大于10;
            if(i<10){
                i=&#39;0&#39;+i;      //如果小于10,则前面+0
            }
                return i;
        }
        function show(){
            var mydate=new Date();
            var year=mydate.getFullYear();
            var month=mydate.getMonth()+1;//因为月份是从零开始,故要得到当前的月份就要+1
            var date=mydate.getDate();  //日
            var day=mydate.getDay();//返回星期几
            var week=new Array(7);
                week[0]=&#39;星期一&#39;;
                week[1]=&#39;星期二&#39;;
                week[2]=&#39;星期三&#39;;
                week[3]=&#39;星期四&#39;;
                week[4]=&#39;星期五&#39;;
                week[5]=&#39;星期六&#39;;
                week[6]=&#39;星期日&#39;;
            var h=mydate.getHours();
            var m=mydate.getMinutes();
            var s=mydate.getSeconds();
            m=checkTime(m);
            s=checkTime(s);
            document.getElementById(&#39;box&#39;).innerHTML=year+&#39;年&#39;+month+&#39;月&#39;+date+&#39;日&#39;+week[day]+h+&#39;:&#39;+m+&#39;:&#39;+s;
        }
        setInterval(show,500);  //每隔500ms调用一次show函数

    };
</script>
</body>
</html>
Copy after login

The current time will be displayed. Note: getday(); returns a number, so it needs to be represented by an array.

Related recommendations:

js gets the current time (including today, tomorrow and yesterday)

js gets the current time (yesterday, today, tomorrow )

PHP method to get the current time

The above is the detailed content of JS gets the current time instance code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template