编程语言
首页 > 编程语言> > javascript输出当前日期(年、月、日、星期、时、分、秒)

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

作者:互联网

获取系统时间是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