{"id":1168406,"date":"2025-01-15T15:57:35","date_gmt":"2025-01-15T07:57:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168406.html"},"modified":"2025-01-15T15:57:38","modified_gmt":"2025-01-15T07:57:38","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae24%e5%b0%8f%e6%97%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1168406.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e24\u5c0f\u65f6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065653\/7b94540f-4a48-48a2-b61a-3356802ac9f7.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e24\u5c0f\u65f6\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u548c\u5904\u740624\u5c0f\u65f6\u683c\u5f0f\u7684\u65f6\u95f4\uff0c\u5982\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001strftime\u65b9\u6cd5\u7b49\u3002<\/strong>\u3001<strong>\u5176\u4e2d\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528datetime\u6a21\u5757<\/strong>\u3001<strong>\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u65f6\u95f4\u663e\u793a<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5904\u740624\u5c0f\u65f6\u683c\u5f0f\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001DATETIME\u6a21\u5757<\/h3>\n<\/p>\n<p><p>datetime\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86datetime\u7c7b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165datetime\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165datetime\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528datetime.datetime.now()\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">now = datetime.datetime.now()<\/p>\n<p>print(&quot;Current Date and Time: &quot;, now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bbe\u7f6e24\u5c0f\u65f6\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528strftime\u65b9\u6cd5\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a24\u5c0f\u65f6\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_time = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;Formatted Date and Time (24-hour): &quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728strftime\u65b9\u6cd5\u4e2d\uff0c%Y\u8868\u793a\u56db\u4f4d\u6570\u7684\u5e74\u4efd\uff0c%m\u8868\u793a\u4e24\u4f4d\u6570\u7684\u6708\u4efd\uff0c%d\u8868\u793a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0c%H\u8868\u793a24\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6\uff0c%M\u8868\u793a\u5206\u949f\uff0c%S\u8868\u793a\u79d2\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001TIME\u6a21\u5757<\/h3>\n<\/p>\n<p><p>time\u6a21\u5757\u4e5f\u662fPython\u6807\u51c6\u5e93\u4e2d\u5904\u7406\u65f6\u95f4\u7684\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u548c\u683c\u5f0f\u5316\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165time\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165time\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528time.localtime()\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">current_time = time.localtime()<\/p>\n<p>print(&quot;Current Time: &quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bbe\u7f6e24\u5c0f\u65f6\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528strftime\u65b9\u6cd5\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a24\u5c0f\u65f6\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, current_time)<\/p>\n<p>print(&quot;Formatted Time (24-hour): &quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pytz\u6a21\u5757\u5904\u7406\u65f6\u533a<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u4e0d\u540c\u7684\u65f6\u533a\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528pytz\u6a21\u5757\u3002pytz\u6a21\u5757\u662fPython\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u65f6\u533a\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5pytz\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5pytz\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pytz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165pytz\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165pytz\u6a21\u5757\u548cdatetime\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pytz<\/p>\n<p>import datetime<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u83b7\u53d6\u6307\u5b9a\u65f6\u533a\u7684\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pytz.timezone()\u65b9\u6cd5\u83b7\u53d6\u6307\u5b9a\u65f6\u533a\u7684\u5f53\u524d\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">timezone = pytz.timezone(&quot;Asia\/Shangh<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>&quot;)<\/p>\n<p>current_time = datetime.datetime.now(timezone)<\/p>\n<p>print(&quot;Current Date and Time in Shanghai: &quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u8bbe\u7f6e24\u5c0f\u65f6\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528strftime\u65b9\u6cd5\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a24\u5c0f\u65f6\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_time = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;Formatted Date and Time in Shanghai (24-hour): &quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528dateutil\u6a21\u5757\u89e3\u6790\u5b57\u7b26\u4e32\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>dateutil\u6a21\u5757\u662fPython\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u7528\u4e8e\u89e3\u6790\u5b57\u7b26\u4e32\u65f6\u95f4\u548c\u5904\u7406\u65e5\u671f\u65f6\u95f4\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5dateutil\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5dateutil\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-dateutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165dateutil\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165dateutil\u6a21\u5757\u548cdatetime\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil import parser<\/p>\n<p>import datetime<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u89e3\u6790\u5b57\u7b26\u4e32\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528parser.parse()\u65b9\u6cd5\u89e3\u6790\u5b57\u7b26\u4e32\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_str = &quot;2023-10-08 14:30:00&quot;<\/p>\n<p>parsed_time = parser.parse(date_str)<\/p>\n<p>print(&quot;Parsed Date and Time: &quot;, parsed_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u8bbe\u7f6e24\u5c0f\u65f6\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528strftime\u65b9\u6cd5\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a24\u5c0f\u65f6\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_time = parsed_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;Formatted Date and Time (24-hour): &quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8bbe\u7f6e\u548c\u5904\u740624\u5c0f\u65f6\u683c\u5f0f\u7684\u65f6\u95f4\u3002<strong>\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u683c\u5f0f\u7684\u65f6\u95f4\u663e\u793a<\/strong>\u3002\u6b64\u5916\uff0ctime\u6a21\u5757\u3001pytz\u6a21\u5757\u548cdateutil\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u529f\u80fd\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4f7f\u7528\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u66f4\u597d\u5730\u5904\u7406\u65f6\u95f4\u548c\u65e5\u671f\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\u768424\u5c0f\u65f6\u5236\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u5c06\u5176\u683c\u5f0f\u5316\u4e3a24\u5c0f\u65f6\u5236\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ncurrent_time = datetime.now().strftime(&quot;%H:%M:%S&quot;)\nprint(&quot;\u5f53\u524d\u65f6\u95f4\uff0824\u5c0f\u65f6\u5236\uff09:&quot;, current_time)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u5f53\u524d\u65f6\u95f4\uff0c\u4ee5\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7684\u5f62\u5f0f\u663e\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c0612\u5c0f\u65f6\u5236\u65f6\u95f4\u8f6c\u6362\u4e3a24\u5c0f\u65f6\u5236\uff1f<\/strong><br \/>\u8981\u5c0612\u5c0f\u65f6\u5236\u7684\u65f6\u95f4\u8f6c\u6362\u4e3a24\u5c0f\u65f6\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strptime<\/code>\u548c<code>strftime<\/code>\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ntime_12_hour = &quot;02:30 PM&quot;\ntime_24_hour = datetime.strptime(time_12_hour, &quot;%I:%M %p&quot;).strftime(&quot;%H:%M&quot;)\nprint(&quot;\u8f6c\u6362\u540e\u7684\u65f6\u95f4\uff0824\u5c0f\u65f6\u5236\uff09:&quot;, time_24_hour)\n<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c\u8f93\u5165\u4e3a12\u5c0f\u65f6\u5236\u7684\u65f6\u95f4\uff0c\u8f93\u51fa\u4e3a\u5bf9\u5e94\u768424\u5c0f\u65f6\u5236\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\uff0c\u4ee524\u5c0f\u65f6\u4e3a\u5355\u4f4d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>schedule<\/code>\u5e93\u6765\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u6bcf24\u5c0f\u65f6\u6267\u884c\u4e00\u6b21\u7279\u5b9a\u7684\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">import schedule\nimport time\n\ndef job():\n    print(&quot;\u4efb\u52a1\u6267\u884c\u65f6\u95f4:&quot;, datetime.now().strftime(&quot;%H:%M:%S&quot;))\n\nschedule.every(24).hours.do(job)\n\nwhile True:\n    schedule.run_pending()\n    time.sleep(1)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>job<\/code>\u51fd\u6570\uff0c\u6bcf24\u5c0f\u65f6\u6267\u884c\u4e00\u6b21\uff0c\u9002\u5408\u5b9a\u671f\u4efb\u52a1\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u548c\u5904\u740624\u5c0f\u65f6\u683c\u5f0f\u7684\u65f6\u95f4\uff0c\u5982\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001strf [&hellip;]","protected":false},"author":3,"featured_media":1168410,"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\/1168406"}],"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=1168406"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168406\/revisions"}],"predecessor-version":[{"id":1168411,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168406\/revisions\/1168411"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168410"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}