<JavaScript> 三. Date对象的属性和方法

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <!-- 整个网页刷新 -->
 5 <!-- <meta http-equiv="refresh" content="1"> -->
 6     <title></title>
 7 <script type="text/javascript">
 8 /*
 9     Date对象
10 */
11 
12 // new 只能使用new关键字创建实例对象
13 // (1) 创建当前时期对象
14 var now = new Date();
15 document.write(now);
16 document.write("<hr>");
17 
18 // (2) 创建时间戳日期对象 距离1970/1/1 0:0:0多少毫秒
19 var timer = new Date(999999999999);
20 document.write(timer);
21 document.write("<hr>");
22 
23 // (3) 创建字符串日期对象
24 var timer = new Date("2017/2/4");
25 document.write(timer);
26 document.write("<hr>");
27 
28 // 实例: 活了多少天
29 var now = new Date();
30 
31 // 换算时间戳
32 var timer1 = now.getTime();
33 
34 // 生日
35 var birthday = new Date("1993/1/1");
36 
37 // 换算时间戳
38 var timer2 = birthday.getTime();
39 
40 // 计算时间
41 var str = (timer1 - timer2) / 1000 / 3600 / 24 / 365;
42 
43 // 输出结果
44 document.write(str);
45 document.write("<hr>");
46 
47 // ------------------- 常用方法 ------------------------
48 //
49 var now = new Date();
50 document.write(now.getFullYear());
51 document.write("<hr>");
52 
53 //
54 document.write(now.getMonth());
55 document.write("<hr>");
56 
57 //
58 document.write(now.getDate());
59 document.write("<hr>");
60 
61 //
62 document.write(now.getHours());
63 document.write("<hr>");
64 
65 //
66 document.write(now.getMinutes());
67 document.write("<hr>");
68 
69 //
70 document.write(now.getSeconds());
71 document.write("<hr>");
72 
73 // 毫秒
74 document.write(now.getMilliseconds());
75 document.write("<hr>");
76 
77 // 星期
78 document.write(now.getDay());
79 document.write("<hr>");
80 
81 // 时间戳
82 document.write(now.getTime());
83 document.write("<hr>");
84 
85 </script>
86 </head>
87 <body>
88 
89 </body>
90 </html>
原文地址:https://www.cnblogs.com/ZeroHour/p/6364626.html