{"id":965627,"date":"2024-12-27T04:39:53","date_gmt":"2024-12-26T20:39:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/965627.html"},"modified":"2024-12-27T04:39:56","modified_gmt":"2024-12-26T20:39:56","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%be%97%e7%b3%bb%e7%bb%9f%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/965627.html","title":{"rendered":"python\u5982\u4f55\u83b7\u5f97\u7cfb\u7edf\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181833\/88385728-efe1-4dd9-80f8-849b11bbb27e.webp\" alt=\"python\u5982\u4f55\u83b7\u5f97\u7cfb\u7edf\u65f6\u95f4\" \/><\/p>\n<p><p> Python\u83b7\u5f97\u7cfb\u7edf\u65f6\u95f4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>pytz<\/code>\u6a21\u5757<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>time<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u529f\u80fd\u3002\u901a\u8fc7<code>time<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\u6233\u3001\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/li>\n<\/ol>\n<p><p><code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>time()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u65f6\u95f4\u6233\u662f\u81ea1970\u5e741\u67081\u65e5\uff08\u79f0\u4e3aUnix\u65f6\u95f4\uff09\u4ee5\u6765\u7ecf\u8fc7\u7684\u79d2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>current_timestamp = time.time()<\/p>\n<p>print(&quot;Current Timestamp:&quot;, current_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u83b7\u53d6\u672c\u5730\u65f6\u95f4<\/li>\n<\/ol>\n<p><p><code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>localtime()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\u7684<code>struct_time<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">local_time = time.localtime()<\/p>\n<p>print(&quot;Local Time:&quot;, local_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u683c\u5f0f\u5316\u65f6\u95f4<\/li>\n<\/ol>\n<p><p><code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>strftime()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06<code>struct_time<\/code>\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u4eba\u7c7b\u53ef\u8bfb\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, local_time)<\/p>\n<p>print(&quot;Formatted Time:&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u4e3a\u76f4\u89c2\u548c\u5f3a\u5927\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\uff0c\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u63a8\u8350\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/li>\n<\/ol>\n<p><p><code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>datetime.now()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u672c\u5730\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>current_datetime = datetime.now()<\/p>\n<p>print(&quot;Current Date and Time:&quot;, current_datetime)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>date()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">current_date = current_datetime.date()<\/p>\n<p>print(&quot;Current Date:&quot;, current_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">current_time = current_datetime.time()<\/p>\n<p>print(&quot;Current Time:&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/li>\n<\/ol>\n<p><p><code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>strftime()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_datetime = current_datetime.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;Formatted Date and Time:&quot;, formatted_datetime)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>pytz<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pytz<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u65f6\u533a\u76f8\u5173\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u533a\u4fe1\u606f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u8de8\u65f6\u533a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5<code>pytz<\/code>\u6a21\u5757<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528<code>pytz<\/code>\u6a21\u5757\u4e4b\u524d\uff0c\u9700\u8981\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install pytz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u83b7\u53d6\u7279\u5b9a\u65f6\u533a\u7684\u65f6\u95f4<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>pytz<\/code>\u6a21\u5757\u53ef\u4ee5\u83b7\u53d6\u4e0d\u540c\u65f6\u533a\u7684\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u83b7\u53d6UTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc_time = datetime.now(pytz.utc)<\/p>\n<p>print(&quot;UTC Time:&quot;, utc_time)<\/p>\n<h2><strong>\u83b7\u53d6\u7279\u5b9a\u65f6\u533a\u7684\u65f6\u95f4\uff08\u4f8b\u5982\uff1a\u7f8e\u56fd\u4e1c\u90e8\u65f6\u95f4\uff09<\/strong><\/h2>\n<p>eastern = pytz.timezone(&#39;US\/Eastern&#39;)<\/p>\n<p>eastern_time = datetime.now(eastern)<\/p>\n<p>print(&quot;Eastern Time:&quot;, eastern_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u65f6\u533a\u8f6c\u6362<\/li>\n<\/ol>\n<p><p><code>pytz<\/code>\u6a21\u5757\u8fd8\u652f\u6301\u5c06\u65f6\u95f4\u4ece\u4e00\u4e2a\u65f6\u533a\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u65f6\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\u65f6\u95f4<\/p>\n<p>local_time = datetime.now()<\/p>\n<p>utc_time_converted = local_time.astimezone(pytz.utc)<\/p>\n<p>print(&quot;Converted UTC Time:&quot;, utc_time_converted)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u7075\u6d3b\u5730\u83b7\u53d6\u548c\u5904\u7406\u7cfb\u7edf\u65f6\u95f4\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u548c\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u65f6\u95f4\u6233\u83b7\u53d6\uff0c\u8fd8\u662f\u590d\u6742\u7684\u65f6\u533a\u5904\u7406\uff0cPython\u90fd\u80fd\u591f\u63d0\u4f9b\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u83b7\u53d6\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>datetime<\/code>\u6a21\u5757\u5b9e\u73b0\u3002\u53ea\u9700\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>datetime.now()<\/code>\u51fd\u6570\u5373\u53ef\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import datetime\ncurrent_time = datetime.datetime.now()\nprint(current_time)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f\u4ee5\u663e\u793a\u7cfb\u7edf\u65f6\u95f4\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5229\u7528<code>strftime<\/code>\u65b9\u6cd5\uff0c\u60a8\u80fd\u591f\u81ea\u5b9a\u4e49\u65f6\u95f4\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">formatted_time = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\nprint(formatted_time)\n<\/code><\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u8f93\u51fa\u5c06\u663e\u793a\u4e3a\u201c\u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2\u201d\u7684\u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8c03\u6574\u683c\u5f0f\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u83b7\u53d6\u7cfb\u7edf\u65f6\u95f4\u7684\u5176\u4ed6\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u7cfb\u7edf\u65f6\u95f4\u3002\u901a\u8fc7<code>time.localtime()<\/code>\u548c<code>time.strftime()<\/code>\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u683c\u5f0f\u5316\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\ncurrent_time = time.localtime()\nformatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, current_time)\nprint(formatted_time)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u540c\u6837\u6709\u6548\uff0c\u9002\u5408\u4e8e\u4e0d\u540c\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u5f97\u7cfb\u7edf\u65f6\u95f4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528pytz\u6a21\u5757\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":965639,"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\/965627"}],"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=965627"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965627\/revisions"}],"predecessor-version":[{"id":965640,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965627\/revisions\/965640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/965639"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=965627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=965627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=965627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}