{"id":954867,"date":"2024-12-27T02:05:49","date_gmt":"2024-12-26T18:05:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/954867.html"},"modified":"2024-12-27T02:05:52","modified_gmt":"2024-12-26T18:05:52","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e6%97%b6%e9%97%b4%e5%88%bb%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/954867.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa\u65f6\u95f4\u523b\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094950\/b8ccfc5c-b3ba-45bc-a625-db0581796400.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\u65f6\u95f4\u523b\u5ea6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8f93\u51fa\u65f6\u95f4\u523b\u5ea6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3001<code>time<\/code>\u6a21\u5757\u548c<code>pytz<\/code>\u5e93\u7b49\u5b9e\u73b0\uff0c\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u3001\u64cd\u4f5c\u548c\u663e\u793a\u65f6\u95f4\u3002<code>datetime<\/code>\u6a21\u5757\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\uff0c<code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u65f6\u95f4\u7684\u8bbf\u95ee\uff0c\u800c<code>pytz<\/code>\u5e93\u5219\u7528\u4e8e\u5904\u7406\u65f6\u533a\u3002<\/strong>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u8f93\u51fa\u5404\u79cd\u65f6\u95f4\u523b\u5ea6\uff0c\u4f8b\u5982\u5f53\u524d\u65f6\u95f4\u3001UTC\u65f6\u95f4\u3001\u65f6\u95f4\u6233\u3001\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001DATETIME\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8868\u793a\u548c\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4:&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>datetime.now()<\/code>\u65b9\u6cd5\u8fd4\u56de\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u7cbe\u786e\u5230\u5fae\u79d2\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>2. \u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>strftime()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u683c\u5f0f\u5316\u5f53\u524d\u65f6\u95f4<\/p>\n<p>formatted_time = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4:&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>%Y-%m-%d %H:%M:%S<\/code>\u662f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d<code>%Y<\/code>\u8868\u793a\u56db\u4f4d\u6570\u7684\u5e74\u4efd\uff0c<code>%m<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u6708\u4efd\uff0c<code>%d<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0c<code>%H<\/code>\u8868\u793a24\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6\uff0c<code>%M<\/code>\u8868\u793a\u5206\u949f\uff0c<code>%S<\/code>\u8868\u793a\u79d2\u3002<\/p>\n<\/p>\n<p><h3>3. \u65f6\u95f4\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u8fd8\u652f\u6301\u65e5\u671f\u548c\u65f6\u95f4\u7684\u52a0\u51cf\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import timedelta<\/p>\n<h2><strong>\u6dfb\u52a010\u5929<\/strong><\/h2>\n<p>future_time = current_time + timedelta(days=10)<\/p>\n<p>print(&quot;10\u5929\u540e\u7684\u65f6\u95f4:&quot;, future_time)<\/p>\n<h2><strong>\u51cf\u53bb5\u5c0f\u65f6<\/strong><\/h2>\n<p>past_time = current_time - timedelta(hours=5)<\/p>\n<p>print(&quot;5\u5c0f\u65f6\u524d\u7684\u65f6\u95f4:&quot;, past_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u521b\u5efa<code>timedelta<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u65f6\u95f4\u7684\u52a0\u51cf\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001TIME\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u65f6\u95f4\u7684\u8bbf\u95ee\u548c\u8f6c\u6362\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3001\u6682\u505c\u7a0b\u5e8f\u6267\u884c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>time<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u83b7\u53d6\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u79d2\u6570\uff0c\u5373\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = time.time()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\u6233:&quot;, timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>time.localtime()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4<\/p>\n<p>local_time = time.localtime(timestamp)<\/p>\n<p>print(&quot;\u672c\u5730\u65f6\u95f4:&quot;, time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, local_time))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6682\u505c\u7a0b\u5e8f\u6267\u884c<\/h3>\n<\/p>\n<p><p><code>time.sleep()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6682\u505c\u7a0b\u5e8f\u6267\u884c2\u79d2<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u6682\u505c2\u79d2&quot;)<\/p>\n<p>time.sleep(2)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001PYTZ\u5e93<\/p>\n<\/p>\n<p><p><code>pytz<\/code>\u5e93\u7528\u4e8e\u5904\u7406\u65f6\u533a\uff0c\u5141\u8bb8\u60a8\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u65f6\u533a\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5pytz\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>pytz<\/code>\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5\u5b83\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pytz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u83b7\u53d6\u7279\u5b9a\u65f6\u533a\u7684\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>pytz<\/code>\u5e93\uff0c\u60a8\u53ef\u4ee5\u83b7\u53d6\u7279\u5b9a\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\u53d6\u7279\u5b9a\u65f6\u533a\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>timezone = pytz.timezone(&#39;Asia\/Shangh<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>&#39;)<\/p>\n<p>timezone_time = datetime.now(timezone)<\/p>\n<p>print(&quot;\u4e0a\u6d77\u65f6\u95f4:&quot;, timezone_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u65f6\u533a\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u5c06\u4e00\u4e2a\u65f6\u533a\u7684\u65f6\u95f4\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u65f6\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efaUTC\u65f6\u95f4<\/p>\n<p>utc_time = datetime.now(pytz.utc)<\/p>\n<h2><strong>\u5c06UTC\u65f6\u95f4\u8f6c\u6362\u4e3a\u4e0a\u6d77\u65f6\u95f4<\/strong><\/h2>\n<p>shanghai_time = utc_time.astimezone(pytz.timezone(&#39;Asia\/Shanghai&#39;))<\/p>\n<p>print(&quot;\u8f6c\u6362\u540e\u7684\u4e0a\u6d77\u65f6\u95f4:&quot;, shanghai_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3001<code>time<\/code>\u6a21\u5757\u548c<code>pytz<\/code>\u5e93\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u65f6\u95f4\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b9e\u73b0\u8de8\u65f6\u533a\u7684\u65f6\u95f4\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u60a8\u9700\u8981\u6bd4\u8f83\u4e24\u4e2a\u4e0d\u540c\u65f6\u533a\u7684\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u65f6\u533a<\/strong><\/h2>\n<p>timezone_ny = pytz.timezone(&#39;America\/New_York&#39;)<\/p>\n<p>timezone_shanghai = pytz.timezone(&#39;Asia\/Shanghai&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<h2><strong>\u5c06\u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3a\u4e0d\u540c\u65f6\u533a\u65f6\u95f4<\/strong><\/h2>\n<p>ny_time = current_time.astimezone(timezone_ny)<\/p>\n<p>shanghai_time = current_time.astimezone(timezone_shanghai)<\/p>\n<h2><strong>\u6bd4\u8f83\u4e24\u4e2a\u65f6\u533a\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>if ny_time &lt; shanghai_time:<\/p>\n<p>    print(&quot;\u7ebd\u7ea6\u65f6\u95f4\u6bd4\u4e0a\u6d77\u65f6\u95f4\u65e9&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u7ebd\u7ea6\u65f6\u95f4\u6bd4\u4e0a\u6d77\u65f6\u95f4\u665a&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u65f6\u95f4\u6570\u636e\u65f6\uff0c\u6709\u65f6\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65f6\u95f4\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u65f6\u95f4\u5b57\u7b26\u4e32<\/p>\n<p>time_str = &quot;2023-10-01 12:00:00&quot;<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/strong><\/h2>\n<p>time_obj = datetime.strptime(time_str, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u8f6c\u6362\u540e\u7684datetime\u5bf9\u8c61:&quot;, time_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5904\u7406\u65f6\u95f4\u5dee<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u4e4b\u95f4\u7684\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u65f6\u95f4\u70b9<\/strong><\/h2>\n<p>start_time = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>end_time = datetime(2023, 10, 5, 15, 30, 0)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>time_difference = end_time - start_time<\/p>\n<p>print(&quot;\u65f6\u95f4\u5dee:&quot;, time_difference)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u5229\u7528Python\u4e2d\u7684<code>datetime<\/code>\u6a21\u5757\u3001<code>time<\/code>\u6a21\u5757\u548c<code>pytz<\/code>\u5e93\u6765\u8f93\u51fa\u548c\u64cd\u4f5c\u65f6\u95f4\u523b\u5ea6\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u548c\u683c\u5f0f\u5316\u5f53\u524d\u65f6\u95f4\uff0c\u8fd8\u80fd\u8fdb\u884c\u65f6\u533a\u8f6c\u6362\u3001\u65f6\u95f4\u8fd0\u7b97\u4ee5\u53ca\u65f6\u95f4\u5dee\u8ba1\u7b97\u7b49\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u5c06\u5728\u6570\u636e\u5904\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u8c03\u8bd5\u7b49\u65b9\u9762\u5e26\u6765\u6781\u5927\u7684\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u683c\u5f0f\u5316\u8f93\u51fa\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\nnow = datetime.now()\nformatted_time = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\nprint(&quot;\u5f53\u524d\u65f6\u95f4:&quot;, formatted_time)\n<\/code><\/pre>\n<p>\u901a\u8fc7<code>strftime<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982&quot;%Y-%m-%d %H:%M:%S&quot;\u8868\u793a&quot;\u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2&quot;\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u65f6\u95f4\u523b\u5ea6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u65f6\u95f4\u523b\u5ea6\u3002<code>pandas<\/code>\u7684<code>date_range<\/code>\u51fd\u6570\u5141\u8bb8\u7528\u6237\u751f\u6210\u6307\u5b9a\u9891\u7387\u7684\u65f6\u95f4\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ntime_scale = pd.date_range(start=&#39;2023-01-01&#39;, end=&#39;2023-01-10&#39;, freq=&#39;D&#39;)\nprint(time_scale)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u751f\u6210\u4ece2023\u5e741\u67081\u65e5\u52302023\u5e741\u670810\u65e5\u7684\u6bcf\u65e5\u65f6\u95f4\u523b\u5ea6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u65f6\u95f4\u5dee\uff1f<\/strong><br \/>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u65f6\u95f4\u5dee\u3002\u901a\u8fc7\u5c06\u4e24\u4e2a<code>datetime<\/code>\u5bf9\u8c61\u76f8\u51cf\uff0c\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\uff0c\u8868\u793a\u4e24\u8005\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\nstart_time = datetime(2023, 1, 1, 12, 0)\nend_time = datetime(2023, 1, 2, 12, 0)\ntime_difference = end_time - start_time\nprint(&quot;\u65f6\u95f4\u5dee:&quot;, time_difference)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5c06\u663e\u793a\u201c\u65f6\u95f4\u5dee: 1 day, 0:00:00\u201d\uff0c\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u4e4b\u95f4\u7684\u5dee\u5f02\u4e3a1\u5929\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u51fa\u65f6\u95f4\u523b\u5ea6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u548cpytz\u5e93\u7b49\u5b9e\u73b0\uff0c\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u591a [&hellip;]","protected":false},"author":3,"featured_media":954876,"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\/954867"}],"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=954867"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954867\/revisions"}],"predecessor-version":[{"id":954877,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/954867\/revisions\/954877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/954876"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=954867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=954867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=954867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}