Javascript学习笔记(11) --日期对象

Js中的日期对象

日期对象的创建new Date()

日期对象的方法 

语法:日期对象.方法()

1>    getTime() 时间戳  从1970年1月 1日 开始到现在的毫秒数

2>    getFullYear() 年

3>    getMonth() 月  中国 1—12   国外 0 ---11   用到month +1

4>    getDate() 日

5>    getDay() 星期

6>    getHour()  小时

7>    getMinutes()  分钟

8>    getSeconds()  秒  

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			*{margin:0;padding: 0; background: #000;color: #fff;font-size: 80px;}
			span{ }
			
		</style>
	</head>
	<body>
		<span id="hours">00</span> : 
		<span id="minutes">00</span> : 
		<span id="seconds">00</span> 
		<script src="script.js"></script>
	</body>
</html>
//日期对象的创建
/*
var date = new Date()
//alert(date)
//日期对象的方法
document.write(date.getTime()+'<br />');//时间戳
document.write(date.getFullYear()+'<br />');//年
document.write(date.getMonth()+'<br />');// 月
document.write(date.getDate()+'<br />');// 日
document.write(date.getDay()+'<br />');// 星期几
*/
//document.write(date.getHours()+'----'+date.getMinutes()+ "---"+date.getSeconds());// 星期几
//案例
var hours = document.getElementById("hours"),
	minutes = document.getElementById("minutes"),
	senconds = document.getElementById("seconds");
	
function fn(){
	var date = new Date();
	hours.innerHTML = time(date.getHours());
	minutes.innerHTML = time(date.getMinutes());
	senconds.innerHTML = time(date.getSeconds());
}
fn();  // 刷新页面出现时间 不用等1s
setInterval(function(){
	fn();
},1000)


//如果时间是单位数 要变成双位数
function time(t){
	if(t<10){
		return t = "0"+t;
	}else{
		return t;
	}
}


猜你喜欢

转载自blog.csdn.net/weixin_39209728/article/details/80666578