{"id":27,"date":"2007-05-21T00:23:23","date_gmt":"2007-05-21T04:23:23","guid":{"rendered":"http:\/\/prglab.com\/blog\/?page_id=27"},"modified":"2010-09-30T10:19:30","modified_gmt":"2010-09-30T14:19:30","slug":"js-date","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-date","title":{"rendered":"JavaScript\u4e2d\u7684\u65e5\u671f(Date)"},"content":{"rendered":"<p>\u65e5\u671f\u5bf9\u8c61Date object\u662f\u7528\u6765\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u64cd\u4f5c\u7684\u3002<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<strong>\u5b9a\u4e49\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61<\/strong><\/p>\n<p>\u4f7f\u7528\u5173\u952e\u8bcdnew\u6765\u5b9a\u4e49\u4e00\u4e2aDate\u5bf9\u8c61\uff0c\u5982\u4e0b\u9762\u4f8b\u5b50\u4e2d\u6240\u793a\uff0c\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3amyDate\u7684\u65e5\u671f\u5bf9\u8c61\uff1a<\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">var myDate=new Date()<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u65e5\u671f\u5bf9\u8c61\u4f1a\u81ea\u52a8\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u4e3a\u521d\u59cb\u503c\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n<strong>\u5bf9\u65e5\u671f\u8fdb\u884c\u64cd\u4f5c<\/strong><\/p>\n<p>\u4f7f\u7528Date\u5bf9\u8c61\u7684\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u7684\u5bf9\u65e5\u671f\u8fdb\u884c\u64cd\u4f5c\u3002<br \/>\n\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\u4e3a\u4e00\u4e2a\u6307\u5b9a\u7684\u65e5\u671f\u503c(2010\u5e741\u670814\u65e5\uff09\uff1a<\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">var myDate=new Date()<br \/>\nmyDate.setFullYear(2010,0,14)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u5c06myDate\u7684\u503c\u8bbe\u4e3a5\u5929\u540e\uff1a<\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">var myDate=new Date()<br \/>\nmyDate.setDate(myDate.getDate()+5)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u5982\u679c\u589e\u52a05\u5929\u540e\u8fdb\u5165\u53e6\u5916\u4e00\u4e2a\u6708\u6216\u4e00\u5e74\uff0cDate\u5bf9\u8c61\u4f1a\u81ea\u52a8\u5904\u7406\u7684\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>\u5bf9\u65e5\u671f\u8fdb\u884c\u6bd4\u8f83<\/strong><\/p>\n<p>\u4f7f\u7528Date\u5bf9\u8c61\u53ef\u4ee5\u5bf9\u65e5\u671f\u8fdb\u884c\u6bd4\u8f83\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c06\u4eca\u5929\u7684\u65e5\u671f\u4e0e2010\u5e741\u670814\u65e5\u8fdb\u884c\u6bd4\u8f83\uff1a<\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">var myDate=new Date()<br \/>\nmyDate.setFullYear(2010,0,14)var today = new Date()<\/p>\n<p>if (myDate&gt;today)<br \/>\nalert(&#8220;\u4eca\u5929\u8fd8\u6ca1\u52302010\u5e741\u670814\u65e5&#8221;)<br \/>\nelse<br \/>\nalert(&#8220;\u4eca\u5929\u5df2\u7ecf\u8fc7\u4e862010\u5e741\u670814\u65e5&#8221;)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<strong>\u4f8b\uff1a\u663e\u793a\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>document.write(Date())<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>\u4f8b\uff1agetTime()\u51fd\u6570<\/strong><br \/>\n\u4f7f\u7528getTime()\u51fd\u6570\u8ba1\u7b97\u81ea1970\u5e74\u540e\u5df2\u7ecf\u8fc7\u4e86\u591a\u5c11\u5e74<\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>var minutes = 1000*60<br \/>\nvar hours = minutes*60<br \/>\nvar days = hours*24<br \/>\nvar years = days*365<br \/>\nvar d = new Date()<br \/>\nvar t = d.getTime()<br \/>\nvar y = t\/years<\/p>\n<p>document.write(&#8220;It&#8217;s been: &#8221; + y + &#8221; years since 1970\/01\/01!&#8221;)<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<strong>\u4f8b\uff1a\u51fd\u6570setFullYear()<\/strong><br \/>\n\u4f7f\u7528setFullYear()\u51fd\u6570\u6765\u8bbe\u7f6e\u4e00\u4e2a\u65e5\u671f<\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>var d = new Date()<br \/>\nd.setFullYear(1992,10,3)<br \/>\ndocument.write(d)<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>\u4f8b\uff1a\u51fd\u6570toUTCString()<\/strong><br \/>\n\u4f7f\u7528toUTCString()\u51fd\u6570\u5c06\u4eca\u5929\u7684\u65e5\u671f\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32<\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>var d = new Date()<br \/>\ndocument.write (d.toUTCString())<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>\u4f8b\uff1a\u51fd\u6570getDay()<\/strong><br \/>\n\u4f7f\u7528getDay()\u51fd\u6570\u548c\u4e00\u4e2a\u6570\u7ec4\u6765\u5199\u51fa\u661f\u671f\u51e0<\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>var d=new Date()<br \/>\nvar weekday=new Array(7)<br \/>\nweekday[0]=&#8221;Sunday&#8221;<br \/>\nweekday[1]=&#8221;Monday&#8221;<br \/>\nweekday[2]=&#8221;Tuesday&#8221;<br \/>\nweekday[3]=&#8221;Wednesday&#8221;<br \/>\nweekday[4]=&#8221;Thursday&#8221;<br \/>\nweekday[5]=&#8221;Friday&#8221;<br \/>\nweekday[6]=&#8221;Saturday&#8221;<\/p>\n<p>document.write(&#8220;Today it is &#8221; + weekday[d.getDay()])<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<strong>\u4f8b\uff1a\u663e\u793a\u4e00\u4e2a\u65f6\u949f<\/strong><\/p>\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"3\" width=\"96%\" align=\"center\">\n<tbody>\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nfunction startTime()<br \/>\n{<br \/>\nvar today=new Date()<br \/>\nvar h=today.getHours()<br \/>\nvar m=today.getMinutes()<br \/>\nvar s=today.getSeconds()<br \/>\n\/\/ add a zero in front of numbers&lt;10<br \/>\nm=checkTime(m)<br \/>\ns=checkTime(s)<br \/>\ndocument.getElementById(&#8216;txt&#8217;).innerHTML=h+&#8221;:&#8221;+m+&#8221;:&#8221;+s<br \/>\nt=setTimeout(&#8216;startTime()&#8217;,500)<br \/>\n}function checkTime(i)<br \/>\n{<br \/>\nif (i&lt;10)<br \/>\n{i=&#8221;0&#8243; + i}<br \/>\nreturn i<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<\/p>\n<p>&lt;body onload=&#8221;startTime()&#8221;&gt;<br \/>\n&lt;div id=&#8221;txt&#8221;&gt;&lt;\/div&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u65e5\u671f\u5bf9\u8c61Date object\u662f\u7528\u6765\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u64cd\u4f5c\u7684\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u5b9a\u4e49\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61 \u4f7f\u7528\u5173\u952e\u8bcdnew\u6765\u5b9a\u4e49\u4e00\u4e2aDate\u5bf9\u8c61\uff0c\u5982\u4e0b\u9762\u4f8b\u5b50\u4e2d\u6240\u793a\uff0c\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3amyDate\u7684\u65e5\u671f\u5bf9\u8c61\uff1a var myDate=new Date() \u6ce8\u610f\uff1a\u65e5\u671f\u5bf9\u8c61\u4f1a\u81ea\u52a8\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u4e3a\u521d\u59cb\u503c\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- \u5bf9\u65e5\u671f\u8fdb\u884c\u64cd\u4f5c \u4f7f\u7528Date\u5bf9\u8c61\u7684\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u7684\u5bf9\u65e5\u671f\u8fdb\u884c\u64cd\u4f5c\u3002 \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\u4e3a\u4e00\u4e2a\u6307\u5b9a\u7684\u65e5\u671f\u503c(2010\u5e741\u670814\u65e5\uff09\uff1a var myDate=new Date() myDate.setFullYear(2010,0,14) \u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u5c06myDate\u7684\u503c\u8bbe\u4e3a5\u5929\u540e\uff1a var myDate=new Date() myDate.setDate(myDate.getDate()+5) \u6ce8\u610f\uff1a\u5982\u679c\u589e\u52a05\u5929\u540e\u8fdb\u5165\u53e6\u5916\u4e00\u4e2a\u6708\u6216\u4e00\u5e74\uff0cDate\u5bf9\u8c61\u4f1a\u81ea\u52a8\u5904\u7406\u7684\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; \u5bf9\u65e5\u671f\u8fdb\u884c\u6bd4\u8f83 \u4f7f\u7528Date\u5bf9\u8c61\u53ef\u4ee5\u5bf9\u65e5\u671f\u8fdb\u884c\u6bd4\u8f83\u3002 \u4e0b\u9762\u7684\u4f8b\u5b50\u5c06\u4eca\u5929\u7684\u65e5\u671f\u4e0e2010\u5e741\u670814\u65e5\u8fdb\u884c\u6bd4\u8f83\uff1a var myDate=new Date() myDate.setFullYear(2010,0,14)var today = new Date() if (myDate&gt;today) alert(&#8220;\u4eca\u5929\u8fd8\u6ca1\u52302010\u5e741\u670814\u65e5&#8221;) else alert(&#8220;\u4eca\u5929\u5df2\u7ecf\u8fc7\u4e862010\u5e741\u670814\u65e5&#8221;) &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u4f8b\uff1a\u663e\u793a\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4 &lt;html&gt; &lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt; document.write(Date()) &lt;\/script&gt; &lt;\/body&gt; &lt;\/html&gt; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; \u4f8b\uff1agetTime()\u51fd\u6570 \u4f7f\u7528getTime()\u51fd\u6570\u8ba1\u7b97\u81ea1970\u5e74\u540e\u5df2\u7ecf\u8fc7\u4e86\u591a\u5c11\u5e74 &lt;html&gt; &lt;body&gt;&lt;script type=&#8221;text\/javascript&#8221;&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":20,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/27"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":6,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/27\/revisions"}],"predecessor-version":[{"id":185,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/27\/revisions\/185"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}