/*
* Date对象
* - 在js中使用Date对象来表示一个时间
*/
//创建一个Date对象
//如果直接是使用构造函数创建一个Date对象,则会封装为当前代码执行的时间
var d = new Date();
//创建一个指定的时间对象
//需要在构造函数中传递一个时间的字符串作为参数
//日期的格式 月份/日/年时:分:秒
var d2 = new Date("12/2/2009 12:25:35")
console.log(d2);
/*
* getDate()
* - 获取当前日期对象是几日
*/
var date = d2.getDate();
console.log(date);
/*
* getDay()
* - 获取当前日期对象是周几
* - 返回值是一个0~6的值
* 0 表示周日
* 1表示周一
* 。。。
*/
var day = d.getDay();
console.log(day);
/*
* getMonth()
* - 获取当前对象的月份
* - 会返回一个0-11的值
* 0 表示1月
* 1表示2月
* ...\
* 11表示12月
*/
var month = d2.getMonth();
console.log(month);
/*
* getFullYear()
* - 获取当前日期对象的年份
*/
var year = d2.getFullYear();
console.log(year);
/*
* getTime()
* - 获取当前日期对象的时间戳
* - 时间戳,是指从格林威治标准时间的1970年1月1日0时0分0秒 到现在的所用的毫秒数(1秒 = 1000毫秒)
* - 计算机在底层保存时间使用是时间戳
*/
var time = d.getTime();
console.log(time/1000/60/60/24/365);
var d3 = new Date("1/1/1970 0:0:0");
time = d3.getTime()
console.log(time);
var start = Date.now();
for(var i =0;i<100;i++ ){
console.log(i)
}
var end = Date.now();
console.log("执行了"+(end-start)+"毫秒");
输出结果:
[Web浏览器] “Wed Dec 02 2009 12:25:35 GMT+0800 (中国标准时间)” /初级教程08/04Date.html (24)
[Web浏览器] “2” /初级教程08/04Date.html (32)
[Web浏览器] “0” /初级教程08/04Date.html (43)
[Web浏览器] “11” /初级教程08/04Date.html (55)
[Web浏览器] “2009” /初级教程08/04Date.html (61)
[Web浏览器] “50.62009883181126” /初级教程08/04Date.html (71)
[Web浏览器] “-28800000” /初级教程08/04Date.html (75)
[Web浏览器] “0” /初级教程08/04Date.html (80)
[Web浏览器] “1” /初级教程08/04Date.html (80)
[Web浏览器] “2” /初级教程08/04Date.html (80)
[Web浏览器] “3” /初级教程08/04Date.html (80)
[Web浏览器] “4” /初级教程08/04Date.html (80)
[Web浏览器] “5” /初级教程08/04Date.html (80)
[Web浏览器] “6” /初级教程08/04Date.html (80)
[Web浏览器] “7” /初级教程08/04Date.html (80)
[Web浏览器] “8” /初级教程08/04Date.html (80)
[Web浏览器] “9” /初级教程08/04Date.html (80)
[Web浏览器] “10” /初级教程08/04Date.html (80)
[Web浏览器] “11” /初级教程08/04Date.html (80)
[Web浏览器] “12” /初级教程08/04Date.html (80)
[Web浏览器] “13” /初级教程08/04Date.html (80)
[Web浏览器] “14” /初级教程08/04Date.html (80)
[Web浏览器] “15” /初级教程08/04Date.html (80)
[Web浏览器] “16” /初级教程08/04Date.html (80)
[Web浏览器] “17” /初级教程08/04Date.html (80)
[Web浏览器] “18” /初级教程08/04Date.html (80)
[Web浏览器] “19” /初级教程08/04Date.html (80)
[Web浏览器] “20” /初级教程08/04Date.html (80)
[Web浏览器] “21” /初级教程08/04Date.html (80)
[Web浏览器] “22” /初级教程08/04Date.html (80)
[Web浏览器] “23” /初级教程08/04Date.html (80)
[Web浏览器] “24” /初级教程08/04Date.html (80)
[Web浏览器] “25” /初级教程08/04Date.html (80)
[Web浏览器] “26” /初级教程08/04Date.html (80)
[Web浏览器] “27” /初级教程08/04Date.html (80)
[Web浏览器] “28” /初级教程08/04Date.html (80)
[Web浏览器] “29” /初级教程08/04Date.html (80)
[Web浏览器] “30” /初级教程08/04Date.html (80)
[Web浏览器] “31” /初级教程08/04Date.html (80)
[Web浏览器] “32” /初级教程08/04Date.html (80)
[Web浏览器] “33” /初级教程08/04Date.html (80)
[Web浏览器] “34” /初级教程08/04Date.html (80)
[Web浏览器] “35” /初级教程08/04Date.html (80)
[Web浏览器] “36” /初级教程08/04Date.html (80)
[Web浏览器] “37” /初级教程08/04Date.html (80)
[Web浏览器] “38” /初级教程08/04Date.html (80)
[Web浏览器] “39” /初级教程08/04Date.html (80)
[Web浏览器] “40” /初级教程08/04Date.html (80)
[Web浏览器] “41” /初级教程08/04Date.html (80)
[Web浏览器] “42” /初级教程08/04Date.html (80)
[Web浏览器] “43” /初级教程08/04Date.html (80)
[Web浏览器] “44” /初级教程08/04Date.html (80)
[Web浏览器] “45” /初级教程08/04Date.html (80)
[Web浏览器] “46” /初级教程08/04Date.html (80)
[Web浏览器] “47” /初级教程08/04Date.html (80)
[Web浏览器] “48” /初级教程08/04Date.html (80)
[Web浏览器] “49” /初级教程08/04Date.html (80)
[Web浏览器] “50” /初级教程08/04Date.html (80)
[Web浏览器] “51” /初级教程08/04Date.html (80)
[Web浏览器] “52” /初级教程08/04Date.html (80)
[Web浏览器] “53” /初级教程08/04Date.html (80)
[Web浏览器] “54” /初级教程08/04Date.html (80)
[Web浏览器] “55” /初级教程08/04Date.html (80)
[Web浏览器] “56” /初级教程08/04Date.html (80)
[Web浏览器] “57” /初级教程08/04Date.html (80)
[Web浏览器] “58” /初级教程08/04Date.html (80)
[Web浏览器] “59” /初级教程08/04Date.html (80)
[Web浏览器] “60” /初级教程08/04Date.html (80)
[Web浏览器] “61” /初级教程08/04Date.html (80)
[Web浏览器] “62” /初级教程08/04Date.html (80)
[Web浏览器] “63” /初级教程08/04Date.html (80)
[Web浏览器] “64” /初级教程08/04Date.html (80)
[Web浏览器] “65” /初级教程08/04Date.html (80)
[Web浏览器] “66” /初级教程08/04Date.html (80)
[Web浏览器] “67” /初级教程08/04Date.html (80)
[Web浏览器] “68” /初级教程08/04Date.html (80)
[Web浏览器] “69” /初级教程08/04Date.html (80)
[Web浏览器] “70” /初级教程08/04Date.html (80)
[Web浏览器] “71” /初级教程08/04Date.html (80)
[Web浏览器] “72” /初级教程08/04Date.html (80)
[Web浏览器] “73” /初级教程08/04Date.html (80)
[Web浏览器] “74” /初级教程08/04Date.html (80)
[Web浏览器] “75” /初级教程08/04Date.html (80)
[Web浏览器] “76” /初级教程08/04Date.html (80)
[Web浏览器] “77” /初级教程08/04Date.html (80)
[Web浏览器] “78” /初级教程08/04Date.html (80)
[Web浏览器] “79” /初级教程08/04Date.html (80)
[Web浏览器] “80” /初级教程08/04Date.html (80)
[Web浏览器] “81” /初级教程08/04Date.html (80)
[Web浏览器] “82” /初级教程08/04Date.html (80)
[Web浏览器] “83” /初级教程08/04Date.html (80)
[Web浏览器] “84” /初级教程08/04Date.html (80)
[Web浏览器] “85” /初级教程08/04Date.html (80)
[Web浏览器] “86” /初级教程08/04Date.html (80)
[Web浏览器] “87” /初级教程08/04Date.html (80)
[Web浏览器] “88” /初级教程08/04Date.html (80)
[Web浏览器] “89” /初级教程08/04Date.html (80)
[Web浏览器] “90” /初级教程08/04Date.html (80)
[Web浏览器] “91” /初级教程08/04Date.html (80)
[Web浏览器] “92” /初级教程08/04Date.html (80)
[Web浏览器] “93” /初级教程08/04Date.html (80)
[Web浏览器] “94” /初级教程08/04Date.html (80)
[Web浏览器] “95” /初级教程08/04Date.html (80)
[Web浏览器] “96” /初级教程08/04Date.html (80)
[Web浏览器] “97” /初级教程08/04Date.html (80)
[Web浏览器] “98” /初级教程08/04Date.html (80)
[Web浏览器] “99” /初级教程08/04Date.html (80)
[Web浏览器] “执行了8毫秒” /初级教程08/04Date.html (84)