{"id":164811,"date":"2024-05-08T15:02:44","date_gmt":"2024-05-08T07:02:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/164811.html"},"modified":"2024-05-08T15:02:53","modified_gmt":"2024-05-08T07:02:53","slug":"python-%e4%b8%8e-javascript-%e4%ba%a4%e6%8d%a2%e6%97%b6%e9%97%b4%e6%95%b0%e6%8d%ae%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/164811.html","title":{"rendered":"Python \u4e0e JavaScript \u4ea4\u6362\u65f6\u95f4\u6570\u636e\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26231739\/51579589-7be7-4792-9fa2-62db78beefdc.webp\" alt=\"Python \u4e0e JavaScript \u4ea4\u6362\u65f6\u95f4\u6570\u636e\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p><strong>Python \u548c JavaScript \u4ea4\u6362\u65f6\u95f4\u6570\u636e\u7684\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\u4f7f\u7528 ISO 8601 \u6807\u51c6\u683c\u5f0f\u3001\u5229\u7528 UNIX \u65f6\u95f4\u6233\u4ee5\u53ca\u786e\u4fdd\u65f6\u533a\u4fe1\u606f\u7684\u51c6\u786e\u6027<\/strong>\u3002ISO 8601 \u6807\u51c6\u63d0\u4f9b\u4e86\u4e00\u79cd\u8de8\u8bed\u8a00\u548c\u5e73\u53f0\u4ea4\u6362\u65f6\u95f4\u6570\u636e\u7684\u901a\u7528\u683c\u5f0f\uff0c\u4f7f\u5f97\u65e0\u8bba\u662f\u5728 Python \u8fd8\u662f JavaScript \u4e2d\uff0c\u90fd\u80fd\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u7406\u89e3\u548c\u5904\u7406\u65f6\u95f4\u4fe1\u606f\u3002\u8fd9\u79cd\u683c\u5f0f\u901a\u5e38\u4ee5YYYY-MM-DDTHH:MM:SSZ\u7684\u5f62\u5f0f\u5c55\u73b0\uff0c\u5176\u4e2dT\u5206\u9694\u65e5\u671f\u548c\u65f6\u95f4\uff0cZ\u6307\u793aUTC\u65f6\u533a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 ISO 8601 \u6807\u51c6\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>ISO 8601 \u6807\u51c6\u683c\u5f0f\u662f\u8fdb\u884c\u65f6\u95f4\u6570\u636e\u4ea4\u6362\u65f6\u7684\u9996\u9009\uff0c\u56e0\u4e3a\u5b83\u88ab\u5e7f\u6cdb\u652f\u6301\uff0c\u5e76\u4e14\u6613\u4e8e\u7406\u89e3\u3002Python \u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>isoformat()<\/code>\u65b9\u6cd5\u6765\u751f\u6210\u8fd9\u4e2a\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u800c JavaScript \u5219\u53ef\u4ee5\u4f7f\u7528<code>Date<\/code>\u5bf9\u8c61\u7684<code>toISOString()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5728Python\u4e2d\uff0c\u9996\u5148\u8981\u786e\u4fdd\u4f60\u6709\u4e00\u4e2a<code>datetime<\/code>\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>isoformat()<\/code>\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5176\u8f6c\u6362\u6210ISO 8601\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>iso_format = now.isoformat()<\/p>\n<p>print(iso_format)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8eJavaScript\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a<code>Date<\/code>\u5bf9\u8c61\uff0c\u4f7f\u7528<code>toISOString()<\/code>\u65b9\u6cd5\u540c\u6837\u53ef\u4ee5\u5f97\u5230ISO 8601\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const now = new Date();<\/p>\n<p>const isoFormat = now.toISOString();<\/p>\n<p>console.log(isoFormat);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528 UNIX \u65f6\u95f4\u6233<\/h3>\n<\/p>\n<p><p>UNIX \u65f6\u95f4\u6233\u662f\u53e6\u4e00\u79cd\u5728Python\u548cJavaScript\u4e4b\u95f4\u4ea4\u6362\u65f6\u95f4\u6570\u636e\u7684\u6709\u6548\u65b9\u6cd5\u3002\u5b83\u8868\u793a\u81ea1970\u5e741\u67081\u65e5\uff08UTC\uff09\u4ee5\u6765\u7ecf\u8fc7\u7684\u79d2\u6570\uff0c\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u65f6\u95f4\u8fd0\u7b97\u6216\u8005\u6bd4\u8f83\u7684\u573a\u666f\u5c24\u4e3a\u6709\u7528\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>time()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u7684UNIX\u65f6\u95f4\u6233\uff0c\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\u3002\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>timestamp = int(time.time())<\/p>\n<p>print(timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u5728JavaScript\u4e2d\uff0c\u901a\u8fc7\u5bf9<code>Date<\/code>\u5bf9\u8c61\u8c03\u7528<code>getTime()<\/code>\u65b9\u6cd5\u5e76\u9664\u4ee51000\uff0c\u53ef\u4ee5\u5f97\u5230UNIX\u65f6\u95f4\u6233\uff08JavaScript\u4e2d\u7684\u65f6\u95f4\u6233\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const timestamp = Math.floor(new Date().getTime() \/ 1000);<\/p>\n<p>console.log(timestamp);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u786e\u4fdd\u65f6\u533a\u4fe1\u606f\u7684\u51c6\u786e\u6027<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u8de8\u65f6\u533a\u7684\u65f6\u95f4\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u65f6\u533a\u4fe1\u606f\u51c6\u786e\u65e0\u8bef\u662f\u975e\u5e38\u5173\u952e\u7684\u3002\u4e0d\u540c\u7684\u5730\u7406\u4f4d\u7f6e\u6709\u4e0d\u540c\u7684\u65f6\u533a\uff0c\u5982\u679c\u5ffd\u7565\u4e86\u65f6\u533a\u4fe1\u606f\uff0c\u5f88\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u89e3\u8bfb\u9519\u8bef\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8ePython\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u6765\u5904\u7406\u65f6\u533a\u3002\u9996\u5148\u5b89\u88c5<code>pytz<\/code>\uff0c\u7136\u540e\u5229\u7528\u5b83\u6765\u521b\u5efa\u65f6\u533a\u610f\u8bc6\u578b\u7684<code>datetime<\/code>\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import pytz<\/p>\n<p>from datetime import datetime<\/p>\n<p>utc = pytz.UTC<\/p>\n<p>now = datetime.now(utc)<\/p>\n<p>print(now.isoformat())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Date<\/code>\u5bf9\u8c61\u81ea\u5e26\u7684\u65b9\u6cd5\u5904\u7406\u65f6\u533a\uff0c\u6216\u8005\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>moment-timezone<\/code>\u6765\u66f4\u4fbf\u6377\u5730\u5904\u7406\u65f6\u533a\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-javascript\">const moment = require(&#039;moment-timezone&#039;);<\/p>\n<p>const now = moment().tz(&quot;UTC&quot;).format();<\/p>\n<p>console.log(now);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u786e\u4fdd\u65f6\u95f4\u6570\u636e\u5728\u4ea4\u6362\u8fc7\u7a0b\u4e2d\u7684\u6807\u51c6\u5316\u3001\u7cbe\u786e\u6027\u4ee5\u53ca\u65f6\u533a\u7684\u4e00\u81f4\u6027\uff0cPython\u548cJavaScript\u4e4b\u95f4\u7684\u65f6\u95f4\u4fe1\u606f\u4ea4\u6362\u53ef\u4ee5\u65e0\u7f1d\u8fdb\u884c\uff0c\u6781\u5927\u5730\u51cf\u5c11\u4e86\u56e0\u65f6\u95f4\u5904\u7406\u4e0d\u5f53\u800c\u5bfc\u81f4\u7684\u9519\u8bef\u548c\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1\uff1aPython \u548c JavaScript \u5982\u4f55\u4ea4\u6362\u65f6\u95f4\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u65f6\u95f4\u6570\u636e\u5728 Python \u548c JavaScript \u4e4b\u95f4\u7684\u4ea4\u6362\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u6765\u5b9e\u73b0\u3002 \u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u6765\u521b\u5efa\u548c\u5904\u7406\u65f6\u95f4\u6570\u636e\u3002\u800c\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u5efa\u7684Date\u5bf9\u8c61\u6765\u64cd\u4f5c\u65f6\u95f4\u3002<\/p>\n<p><strong>Q2\uff1a\u600e\u6837\u5728Python\u548cJavascript\u4e4b\u95f4\u8f6c\u6362\u65f6\u95f4\u6233\uff1f<\/strong><\/p>\n<p>\u8981\u5728Python\u548cJavaScript\u4e4b\u95f4\u8f6c\u6362\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684Unix\u65f6\u95f4\u6233\u3002 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528time\u6a21\u5757\u4e2d\u7684time()\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff0c\u7136\u540e\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u6216\u6574\u6570\u8fdb\u884c\u4f20\u8f93\u3002 \u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Date\u5bf9\u8c61\u7684getTime()\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6216\u6574\u6570\u540e\u8fdb\u884c\u4f20\u8f93\u3002<\/p>\n<p><strong>Q3\uff1aPython\u548cJavaScript\u4e4b\u95f4\u5982\u4f55\u8fdb\u884c\u65f6\u533a\u8f6c\u6362\uff1f<\/strong><\/p>\n<p>\u65f6\u533a\u8f6c\u6362\u5728Python\u548cJavaScript\u4e4b\u95f4\u53ef\u80fd\u6d89\u53ca\u4e0d\u540c\u7684\u5e93\u548c\u65b9\u6cd5\u3002 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pytz\u5e93\u6765\u5904\u7406\u65f6\u533a\u8f6c\u6362\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u533a\u4fe1\u606f\u548c\u51fd\u6570\u6765\u5904\u7406\u65f6\u533a\u8f6c\u6362\u3002 \u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Intl\u5bf9\u8c61\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u6765\u8fdb\u884c\u65f6\u533a\u8f6c\u6362\uff0c\u5982toLocaleString()\u65b9\u6cd5\u6765\u5904\u7406\u672c\u5730\u5316\u65f6\u533a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u548c JavaScript \u4ea4\u6362\u65f6\u95f4\u6570\u636e\u7684\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\u4f7f\u7528 ISO 8601 \u6807\u51c6\u683c\u5f0f\u3001\u5229\u7528 UN [&hellip;]","protected":false},"author":3,"featured_media":164823,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/164811"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=164811"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/164811\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/164823"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=164811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=164811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=164811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}