ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript输出当前日期(年、月、日、星期、时、分、秒)

2020-05-23 10:39:21  阅读:12  来源: 互联网

标签:星期 checkTime min javascript 日期 sec var new today


获取系统时间是Javascript常用模块,做了个比较完整清晰的模块,在此备忘

<script type="text/javascript">
function startTime()
{
	var today=new Date()
	var y=today.getFullYear()
	var mon=today.getMonth()+1
	var d=today.getDate()
	var hour=today.getHours()
	var min=today.getMinutes()
	var sec=today.getSeconds()
	var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
	var w= weeks[today.getDay()]
	min=checkTime(min)
	sec=checkTime(sec)
	document.getElementById('txt').innerHTML=y+" 年 "+mon+" 月 "+d+" 日  "+w+" "+hour+":"+min+":"+sec
	t=setTimeout('startTime()',500)
}

function checkTime(i)
{
	if (i<10)
	{i="0" + i}
	return i
}
</script>

全部

<html>
<head>
<script type="text/javascript">
function startTime()
{
	var today=new Date()
	var y=today.getFullYear()
	var mon=today.getMonth()+1
	var d=today.getDate()
	var hour=today.getHours()
	var min=today.getMinutes()
	var sec=today.getSeconds()
	var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
	var w= weeks[today.getDay()]
	min=checkTime(min)
	sec=checkTime(sec)
	document.getElementById('txt').innerHTML=y+" 年 "+mon+" 月 "+d+" 日  "+w+" "+hour+":"+min+":"+sec
	t=setTimeout('startTime()',500)
}

function checkTime(i)
{
	if (i<10)
	{i="0" + i}
	return i
}
</script>
</head>
<body onl oad="startTime()">
<div id="txt"></div>
</body>
</html>

效果
在这里插入图片描述

标签:星期,checkTime,min,javascript,日期,sec,var,new,today
来源: https://blog.csdn.net/The_Handsome_Sir/article/details/106285067

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有