JS Date


创建日期

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);

 

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