码迷,mamicode.com
首页 > Web开发 > 详细

JS Date

时间:2015-10-24 18:47:51      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:


创建日期

new Date()
new Date(vlaue)
new Date(year, month[, day, [, hour][, minutes[,seconds[, millisecones]]]]])


例子:
  new Date(1998, 11);	// js的月份是从0 开始的。 11就是12月	

  new Date(2001, 9, 11);	

  new Date(2015, 7, 12, 9, 11, 18);	 //完整的

返回年月日的时间。
<script type="text/javascript">
	
	var date = new Date();

	function padding(number) {
		return number < 10 ? "0" + number : number; 
	}

	function format(date) {
		return date.getFullYear() + " - " + 
	 	padding(date.getMonth() + 1) + " - " + 
	 	padding(date.getDate()) + " - " + 
	 	padding(date.getHours()) + " - " + 
	 	padding(date.getMinutes()) + " - " + 
	 	padding(date.getSeconds());
	}

	alert(format(date));
</script>


date.setXXX(); 设定

setFullYear()
setMouth()
setDate()
setHours()
setMinutes()
setSeconds()


如果setDate(35), 就变成了 35 - 当月的长度。 加入到下个月。
比如是8月,35,--》 9月4日。


new Date(2001, 2, 0);		// 2001-2-28 00:00:00;
第0天,就是上一个月的最后一天。

获取一个月的天数
<script type="text/javascript">
	
	function getDays(year, month) {
		var date = new Date(year, month, 0);
		return date.getDate();
	}

	alert("2001年2月有: " + getDays(2001, 2) + " 天。");
	alert("2001年3月有: " + getDays(2001, 3) + " 天。");

</script>


number unix时间戳。
var date = new Date(number);

date.setTime(number);

 

JS Date

标签:

原文地址:http://www.cnblogs.com/hgonlywj/p/4907236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!