{"id":2131,"date":"2025-03-01T00:02:00","date_gmt":"2025-02-28T16:02:00","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2131"},"modified":"2025-03-01T00:02:00","modified_gmt":"2025-02-28T16:02:00","slug":"javascript-date%ef%bc%88%e6%97%a5%e6%9c%9f%ef%bc%89-%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/01\/javascript-date%ef%bc%88%e6%97%a5%e6%9c%9f%ef%bc%89-%e5%af%b9%e8%b1%a1\/","title":{"rendered":"JavaScript Date\uff08\u65e5\u671f\uff09 \u5bf9\u8c61"},"content":{"rendered":"\n<p>\u5728 JavaScript \u4e2d\uff0c<strong>Date<\/strong> \u5bf9\u8c61\u7528\u4e8e\u5904\u7406\u548c\u64cd\u4f5c\u65e5\u671f\u4e0e\u65f6\u95f4\u3002\u5b83\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8fdb\u884c\u65e5\u671f\u7684\u8ba1\u7b97\uff0c\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f\u7b49\u3002<code>Date<\/code> \u5bf9\u8c61\u65e2\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5f53\u524d\u65e5\u671f\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u4efb\u610f\u7684\u65f6\u95f4\u70b9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u521b\u5efa Date \u5bf9\u8c61<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h4>\n\n\n\n<p>\u76f4\u63a5\u8c03\u7528 <code>new Date()<\/code> \u4e0d\u4f20\u5165\u4efb\u4f55\u53c2\u6570\u65f6\uff0c\u8fd4\u56de\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const now = new Date();\nconsole.log(now);  \/\/ \u8f93\u51fa\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u683c\u5f0f\u7c7b\u4f3c\uff1aMon Feb 28 2025 12:34:56 GMT+0000 (UTC)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 <strong>\u6307\u5b9a\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e0d\u540c\u7684\u53c2\u6570\u6765\u521b\u5efa\u6307\u5b9a\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)<\/code> \u5176\u4e2d\uff0c<code>monthIndex<\/code> \u662f\u4ece <code>0<\/code> \u5f00\u59cb\u7684\uff0c\u5373 <code>0<\/code> \u4ee3\u8868\u4e00\u6708\uff0c<code>1<\/code> \u4ee3\u8868\u4e8c\u6708\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u521b\u5efa 2025 \u5e74 2 \u6708 28 \u65e5 12:34:56 \u7684\u65e5\u671f\nconst specificDate = new Date(2025, 1, 28, 12, 34, 56);\nconsole.log(specificDate);  \/\/ \u8f93\u51fa\uff1aFri Feb 28 2025 12:34:56 GMT+0000 (UTC)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.3 <strong>\u901a\u8fc7\u5b57\u7b26\u4e32\u521b\u5efa\u65e5\u671f<\/strong><\/h4>\n\n\n\n<p>\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u65e5\u671f\u5b57\u7b26\u4e32\uff08ISO 8601 \u683c\u5f0f\u6216\u5e38\u89c1\u7684\u65e5\u671f\u683c\u5f0f\uff09\u6765\u521b\u5efa <code>Date<\/code> \u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const dateString = new Date(\"2025-02-28T12:34:56Z\");\nconsole.log(dateString);  \/\/ \u8f93\u51fa\uff1aFri Feb 28 2025 12:34:56 GMT+0000 (UTC)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">1.4 <strong>\u901a\u8fc7\u65f6\u95f4\u6233\u521b\u5efa\u65e5\u671f<\/strong><\/h4>\n\n\n\n<p><code>Date<\/code> \u8fd8\u53ef\u4ee5\u901a\u8fc7\u6beb\u79d2\u7ea7\u522b\u7684\u65f6\u95f4\u6233\u6765\u521b\u5efa\u3002\u65f6\u95f4\u6233\u8868\u793a\u81ea 1970 \u5e74 1 \u6708 1 \u65e5 00:00:00 UTC \u5230\u6307\u5b9a\u65f6\u95f4\u7684\u6beb\u79d2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const timestampDate = new Date(1700000000000);  \/\/ \u65f6\u95f4\u6233\u793a\u4f8b\nconsole.log(timestampDate);  \/\/ \u8f93\u51fa\uff1aSun Dec 01 2024 03:46:40 GMT+0000 (UTC)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u83b7\u53d6\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h3>\n\n\n\n<p>\u4e00\u65e6\u521b\u5efa\u4e86 <code>Date<\/code> \u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u83b7\u53d6\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>getFullYear()<\/code>\uff1a\u83b7\u53d6\u5e74\u4efd\u3002<\/li>\n\n\n\n<li><code>getMonth()<\/code>\uff1a\u83b7\u53d6\u6708\u4efd\uff080-11\uff0c0\u8868\u793a\u4e00\u6708\uff09\u3002<\/li>\n\n\n\n<li><code>getDate()<\/code>\uff1a\u83b7\u53d6\u6708\u4efd\u4e2d\u7684\u65e5\u671f\uff081-31\uff09\u3002<\/li>\n\n\n\n<li><code>getDay()<\/code>\uff1a\u83b7\u53d6\u661f\u671f\u51e0\uff080-6\uff0c0\u8868\u793a\u661f\u671f\u5929\uff09\u3002<\/li>\n\n\n\n<li><code>getHours()<\/code>\uff1a\u83b7\u53d6\u5c0f\u65f6\uff080-23\uff09\u3002<\/li>\n\n\n\n<li><code>getMinutes()<\/code>\uff1a\u83b7\u53d6\u5206\u949f\uff080-59\uff09\u3002<\/li>\n\n\n\n<li><code>getSeconds()<\/code>\uff1a\u83b7\u53d6\u79d2\u6570\uff080-59\uff09\u3002<\/li>\n\n\n\n<li><code>getMilliseconds()<\/code>\uff1a\u83b7\u53d6\u6beb\u79d2\uff080-999\uff09\u3002<\/li>\n\n\n\n<li><code>getTime()<\/code>\uff1a\u83b7\u53d6\u4ece1970\u5e741\u67081\u65e500:00:00 UTC \u5230\u5f53\u524d\u65f6\u95f4\u7684\u6beb\u79d2\u6570\u3002<\/li>\n\n\n\n<li><code>getTimezoneOffset()<\/code>\uff1a\u83b7\u53d6\u5f53\u524d\u65f6\u533a\u76f8\u5bf9\u4e8e UTC \u7684\u504f\u79fb\u91cf\uff08\u5355\u4f4d\u4e3a\u5206\u949f\uff0c\u897f\u65b9\u504f\u79fb\u4e3a\u6b63\u503c\uff0c\u4e1c\u65b9\u504f\u79fb\u4e3a\u8d1f\u503c\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>const now = new Date();\nconsole.log(now.getFullYear());         \/\/ 2025\nconsole.log(now.getMonth());            \/\/ 1 (2\u6708)\nconsole.log(now.getDate());             \/\/ 28\nconsole.log(now.getDay());              \/\/ 5 (\u661f\u671f\u4e94)\nconsole.log(now.getHours());            \/\/ 12 (\u65f6)\nconsole.log(now.getMinutes());          \/\/ 34 (\u5206)\nconsole.log(now.getSeconds());          \/\/ 56 (\u79d2)\nconsole.log(now.getMilliseconds());     \/\/ 789 (\u6beb\u79d2)\nconsole.log(now.getTime());             \/\/ 1677482096789 (\u65f6\u95f4\u6233)\nconsole.log(now.getTimezoneOffset());   \/\/ 0\uff08UTC\u65f6\u533a\u504f\u79fb\uff09<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u83b7\u53d6 UTC \u65f6\u95f4<\/strong><\/h4>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u83b7\u53d6 UTC \u65f6\u95f4\uff08\u534f\u8c03\u4e16\u754c\u65f6\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0e <code>get<\/code> \u65b9\u6cd5\u5bf9\u5e94\u7684 <code>getUTC<\/code> \u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(now.getUTCFullYear());        \/\/ 2025\nconsole.log(now.getUTCMonth());           \/\/ 1 (2\u6708)\nconsole.log(now.getUTCDate());            \/\/ 28\nconsole.log(now.getUTCDay());             \/\/ 5 (\u661f\u671f\u4e94)\nconsole.log(now.getUTCHours());           \/\/ 12\nconsole.log(now.getUTCMinutes());         \/\/ 34\nconsole.log(now.getUTCSeconds());         \/\/ 56\nconsole.log(now.getUTCMilliseconds());    \/\/ 789<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4fee\u6539\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>set<\/code> \u65b9\u6cd5\u6765\u4fee\u6539 <code>Date<\/code> \u5bf9\u8c61\u7684\u5404\u4e2a\u90e8\u5206\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>setFullYear(year, monthIndex, day)<\/code>\uff1a\u8bbe\u7f6e\u5e74\u4efd\u3001\u6708\u4efd\u548c\u65e5\u671f\u3002<\/li>\n\n\n\n<li><code>setMonth(monthIndex, day)<\/code>\uff1a\u8bbe\u7f6e\u6708\u4efd\u548c\u65e5\u671f\u3002<\/li>\n\n\n\n<li><code>setDate(day)<\/code>\uff1a\u8bbe\u7f6e\u65e5\u671f\uff081-31\uff09\u3002<\/li>\n\n\n\n<li><code>setHours(hours, minutes, seconds, milliseconds)<\/code>\uff1a\u8bbe\u7f6e\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u548c\u6beb\u79d2\u3002<\/li>\n\n\n\n<li><code>setMinutes(minutes, seconds, milliseconds)<\/code>\uff1a\u8bbe\u7f6e\u5206\u949f\u3001\u79d2\u548c\u6beb\u79d2\u3002<\/li>\n\n\n\n<li><code>setSeconds(seconds, milliseconds)<\/code>\uff1a\u8bbe\u7f6e\u79d2\u548c\u6beb\u79d2\u3002<\/li>\n\n\n\n<li><code>setMilliseconds(milliseconds)<\/code>\uff1a\u8bbe\u7f6e\u6beb\u79d2\u3002<\/li>\n\n\n\n<li><code>setTime(milliseconds)<\/code>\uff1a\u901a\u8fc7\u65f6\u95f4\u6233\u8bbe\u7f6e\u65e5\u671f\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4fee\u6539\u65e5\u671f<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const date = new Date(2025, 1, 28, 12, 34, 56); \/\/ 2025-02-28 12:34:56\ndate.setFullYear(2026);\ndate.setMonth(0);  \/\/ 1\u6708\ndate.setDate(15);   \/\/ 15\u53f7\n\nconsole.log(date);  \/\/ \u8f93\u51fa\uff1aThu Jan 15 2026 12:34:56 GMT+0000 (UTC)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u65e5\u671f\u6bd4\u8f83<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>Date<\/code> \u5bf9\u8c61\u8fdb\u884c\u65e5\u671f\u6bd4\u8f83\uff0c\u56e0\u4e3a <code>Date<\/code> \u5bf9\u8c61\u4f1a\u81ea\u52a8\u8c03\u7528\u5176 <code>valueOf()<\/code> \u65b9\u6cd5\u8fd4\u56de\u65f6\u95f4\u6233\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const date1 = new Date(2025, 1, 28);\nconst date2 = new Date(2025, 1, 28);\n\nconsole.log(date1 === date2);  \/\/ false\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4e0d\u540c\u7684\u5bf9\u8c61\nconsole.log(date1.getTime() === date2.getTime());  \/\/ true\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u65f6\u95f4\u6233\u76f8\u540c<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u65e5\u671f\u683c\u5f0f\u5316<\/strong><\/h3>\n\n\n\n<p>JavaScript \u4e2d\u6ca1\u6709\u5185\u5efa\u7684\u65e5\u671f\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528 <code>toLocaleDateString()<\/code> \u548c <code>toLocaleTimeString()<\/code> \u65b9\u6cd5\u4ee5\u4e0d\u540c\u7684\u683c\u5f0f\u8f93\u51fa\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>toLocaleDateString(locales, options)<\/code>\uff1a\u6839\u636e\u8bed\u8a00\u73af\u5883\u8f93\u51fa\u65e5\u671f\u3002<\/li>\n\n\n\n<li><code>toLocaleTimeString(locales, options)<\/code>\uff1a\u6839\u636e\u8bed\u8a00\u73af\u5883\u8f93\u51fa\u65f6\u95f4\u3002<\/li>\n\n\n\n<li><code>toLocaleString(locales, options)<\/code>\uff1a\u540c\u65f6\u8f93\u51fa\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>const now = new Date();\n\n\/\/ \u9ed8\u8ba4\u683c\u5f0f\uff08\u6839\u636e\u7cfb\u7edf\u8bed\u8a00\uff09\nconsole.log(now.toLocaleDateString());   \/\/ 2025\/2\/28\nconsole.log(now.toLocaleTimeString());   \/\/ 12:34:56\n\n\/\/ \u81ea\u5b9a\u4e49\u683c\u5f0f\nconst options = { year: 'numeric', month: 'long', day: 'numeric' };\nconsole.log(now.toLocaleDateString('en-US', options));  \/\/ February 28, 2025\nconsole.log(now.toLocaleDateString('zh-CN', options));  \/\/ 2025\u5e742\u670828\u65e5\n\n\/\/ \u8f93\u51fa\u65e5\u671f\u548c\u65f6\u95f4\nconsole.log(now.toLocaleString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }));\n\/\/ Thursday, February 28, 2025<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u65e5\u671f\u8ba1\u7b97<\/strong><\/h3>\n\n\n\n<p>\u5047\u8bbe\u4f60\u60f3\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\uff08\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function calculateDaysBetweenDates(date1, date2) {\n  const msInDay = 1000 * 60 * 60 * 24; \/\/ \u6bcf\u5929\u7684\u6beb\u79d2\u6570\n  const diffInMs = Math.abs(date2 - date1); \/\/ \u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u7684\u5dee\u5f02\uff08\u6beb\u79d2\uff09\n  return Math.floor(diffInMs \/ msInDay);    \/\/ \u5c06\u6beb\u79d2\u8f6c\u4e3a\u5929\u6570\n}\n\nconst date1 = new Date(2025, 1, 28); \/\/ 2025-02-28\nconst date2 = new Date(2025, 2, 5);  \/\/ 2025-03-05\n\nconsole.log(calculateDaysBetweenDates(date1, date2));  \/\/ \u8f93\u51fa\uff1a5<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p><code>Date<\/code> \u5bf9\u8c61\u662f JavaScript \u4e2d\u975e\u5e38\u91cd\u8981\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u8ba1\u7b97\u3001\u683c\u5f0f\u5316\u4ee5\u53ca\u5404\u79cd\u64cd\u4f5c\u3002\u901a\u8fc7 <code>Date<\/code> \u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u3001\u65f6\u95f4\uff0c\u4fee\u6539\u65e5\u671f\uff0c\u8fdb\u884c\u65e5\u671f\u7684\u6bd4\u8f83\u548c\u683c\u5f0f\u5316\u7b49\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d\uff0cDate \u5bf9\u8c61\u7528\u4e8e\u5904\u7406\u548c\u64cd\u4f5c\u65e5\u671f\u4e0e\u65f6\u95f4\u3002\u5b83\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8fdb\u884c\u65e5\u671f\u7684\u8ba1\u7b97 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2131","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2131"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2131\/revisions"}],"predecessor-version":[{"id":2132,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2131\/revisions\/2132"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}