Home > Web Front-end > JS Tutorial > body text

js code implementation to display time on web page

不言
Release: 2018-08-22 11:43:39
Original
3304 people have browsed it

The content of this article is about the code implementation of js displaying time on the web page. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
			<span id="date1">
			</span>
			
		<script >
		
			function $(num1){
				return num1<10?"0"+num1:num1;
			}
			
			function setTime(){
				var date = new Date();
				var year = date.getFullYear();
				var month = date.getMonth()+1;
				var day = date.getDate();//在使用天数的时候使用date来得到
				var week = date.getDay();//getday得到的是一周内的第几天
				console.log();
				var hour = date.getHours();
				var minute = date.getMinutes();
				var second = date.getSeconds();
				var time = year+"-"+$(month)+&#39;-&#39;+$(day)+"  "+$(hour)+":"+$(minute)+":"+$(second);
				document.getElementById("date1").innerHTML=time;
			}
			setTime();
			setInterval(&#39;setTime()&#39;,500);
		</script>
	</body>
</html>
Copy after login

A little trick using javascript is to first get the various times, combine them together through functions, and then use document.getElementById().innerHTML=the combined time. to print.

Note that the function needs to be called once before using setinterval() (to prevent the page from being blank when it is first opened).

Related recommendations:

JavaScript dynamically displays the current date and time on the webpage

js realizes simple display on the webpage Time method_javascript skills

The above is the detailed content of js code implementation to display time on web page. 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