用Js获取当前显示时间

Js初学菜鸟


<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>数字时钟title>
	<style type="text/css">
		#cont{border:2px solid green;width: 400px;height: 50px;text-align: center;line-height: 50px;margin:280px;font-size: 22px;font-family: '楷体';color:green;}
	style>
head>
<body>
	<div id="cont">div>

	<script type="text/javascript">
		var tew=document.getElementById('cont')
        // ti.innerHTML="你好";


        function getTime(){     	//获取时间
        	var date=new Date();

        	var year=date.getFullYear();
        	var month=date.getMonth();
        	var day=date.getDate();

            var hour=date.getHours();
            var minute=date.getMinutes();
            var second=date.getSeconds();

            //这样写显示时间在1~9会挤占空间;所以要在1~9的数字前补零;
            if (hour<10) {
            	hour='0'+hour;
            }
            if (minute<10) {
            	minute='0'+minute;
            }
            if (second<10) {
            	second='0'+second;
            }


            var x=date.getDay();//获取星期


            var time=year+'/'+month+'/'+day+'/'+hour+':'+minute+':'+second
              tew.innerHTML=time;//将时间显示在div内
        }
        getTime();
        setInterval(getTime,1000);
        
	script>
body>
html>

你可能感兴趣的:(html,JavaScript)