{"id":1020463,"date":"2024-12-27T13:12:13","date_gmt":"2024-12-27T05:12:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1020463.html"},"modified":"2024-12-27T13:12:16","modified_gmt":"2024-12-27T05:12:16","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e6%97%b6%e9%97%b4%e9%97%b4%e9%9a%94","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1020463.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25162840\/6065d6d7-fb00-4d5b-9add-c6712548ed03.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\" \/><\/p>\n<p><p> Python\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u4f9d\u8d56\u4e8e\u6807\u51c6\u5e93\u4e2d\u7684<code>datetime<\/code>\u6a21\u5757\u3001<code>time<\/code>\u6a21\u5757\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982<code>pytz<\/code>\u548c<code>dateutil<\/code>\u3002<strong>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong>\u662f\u5e38\u89c1\u7684\u4e09\u79cd\u65b9\u5f0f\u3002\u63a8\u8350\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u5bf9<code>datetime<\/code>\u6a21\u5757\u7684\u8be6\u7ec6\u8bb2\u89e3\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757<\/strong>\uff1a\u8fd9\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u7684\u65b9\u6cd5\u3002<code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5904\u7406\u529f\u80fd\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u95f4\u9694\u3002\u8981\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u4e4b\u95f4\u7684\u65f6\u95f4\u5dee\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime.timedelta<\/code>\u7c7b\u3002\u8fd9\u4e2a\u7c7b\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5929\u6570\u548c\u79d2\u6570\u7684\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e24\u4e2a\u65f6\u95f4\u4e4b\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u65e5\u671f\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>end_time = datetime(2023, 10, 2, 14, 30, 0)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>time_difference = end_time - start_time<\/p>\n<h2><strong>\u6253\u5370\u5929\u6570\u548c\u79d2\u6570<\/strong><\/h2>\n<p>print(f&quot;\u95f4\u9694\u5929\u6570: {time_difference.days}&quot;)<\/p>\n<p>print(f&quot;\u95f4\u9694\u79d2\u6570: {time_difference.seconds}&quot;)<\/p>\n<p>print(f&quot;\u603b\u79d2\u6570: {time_difference.total_seconds()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8ba1\u7b97\u51fa\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5929\u6570\u3001\u79d2\u6570\u6216\u662f\u603b\u79d2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u5f3a\u5927\u7684\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u548c\u65b9\u6cd5\u6765\u5904\u7406\u65e5\u671f\u3001\u65f6\u95f4\u4ee5\u53ca\u5b83\u4eec\u7684\u7ec4\u5408\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<p><p><code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>datetime<\/code>\u7c7b\u7528\u4e8e\u521b\u5efa\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u3002\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u63d0\u4f9b\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u6765\u521b\u5efa\u4e00\u4e2a\u5177\u4f53\u7684\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<\/strong><\/h2>\n<p>dt = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>print(dt)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/li>\n<\/ol>\n<p><p>\u8981\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u53ef\u4ee5\u76f4\u63a5\u76f8\u51cf\uff0c\u5f97\u5230\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\u3002<code>timedelta<\/code>\u5bf9\u8c61\u5305\u542b\u4e86\u95f4\u9694\u7684\u5929\u6570\u548c\u79d2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<\/strong><\/h2>\n<p>start_time = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>end_time = datetime(2023, 10, 2, 14, 30, 0)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>time_difference = end_time - start_time<\/p>\n<h2><strong>\u6253\u5370\u95f4\u9694<\/strong><\/h2>\n<p>print(f&quot;\u95f4\u9694\u5929\u6570: {time_difference.days}, \u95f4\u9694\u79d2\u6570: {time_difference.seconds}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/li>\n<\/ol>\n<p><p><code>datetime<\/code>\u6a21\u5757\u8fd8\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u53ef\u4ee5\u4f7f\u7528<code>datetime.now()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4: {current_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>strftime<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4<\/strong><\/h2>\n<p>formatted_time = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(f&quot;\u683c\u5f0f\u5316\u65f6\u95f4: {formatted_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u89e3\u6790\u5b57\u7b26\u4e32\u4e3a\u65e5\u671f\u65f6\u95f4<\/strong><\/li>\n<\/ol>\n<p><p>\u76f8\u5e94\u5730\uff0c\u901a\u8fc7<code>strptime<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u683c\u5f0f\u5316\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u4e00\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5b9a\u4e49\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_string = &quot;2023-10-01 12:00:00&quot;<\/p>\n<h2><strong>\u89e3\u6790\u5b57\u7b26\u4e32\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<\/strong><\/h2>\n<p>parsed_time = datetime.strptime(time_string, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(f&quot;\u89e3\u6790\u540e\u7684\u65f6\u95f4: {parsed_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>time<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u5404\u79cd\u51fd\u6570\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406Unix\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/strong><\/li>\n<\/ol>\n<p><p><code>time<\/code>\u6a21\u5757\u7684<code>time()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684Unix\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>current_timestamp = time.time()<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4\u6233: {current_timestamp}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65f6\u95f4<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>localtime()<\/code>\u51fd\u6570\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7ed3\u6784\u5316\u7684\u65f6\u95f4\u5bf9\u8c61\uff08<code>struct_time<\/code>\uff09\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>current_timestamp = time.time()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u65f6\u95f4<\/strong><\/h2>\n<p>local_time = time.localtime(current_timestamp)<\/p>\n<p>print(f&quot;\u672c\u5730\u65f6\u95f4: {local_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7ed3\u6784\u5316\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/li>\n<\/ol>\n<p><p>\u76f8\u5e94\u5730\uff0c\u53ef\u4ee5\u901a\u8fc7<code>mktime()<\/code>\u51fd\u6570\u5c06\u4e00\u4e2a\u7ed3\u6784\u5316\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u56de\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u7ed3\u6784\u5316\u65f6\u95f4<\/strong><\/h2>\n<p>local_time = time.localtime()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = time.mktime(local_time)<\/p>\n<p>print(f&quot;\u65f6\u95f4\u6233: {timestamp}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u683c\u5f0f\u5316\u65f6\u95f4<\/strong><\/li>\n<\/ol>\n<p><p><code>asctime()<\/code>\u548c<code>strftime()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u7ed3\u6784\u5316\u65f6\u95f4\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u7ed3\u6784\u5316\u65f6\u95f4<\/strong><\/h2>\n<p>local_time = time.localtime()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4<\/strong><\/h2>\n<p>formatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, local_time)<\/p>\n<p>print(f&quot;\u683c\u5f0f\u5316\u65f6\u95f4: {formatted_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6807\u51c6\u5e93\uff0cPython\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8fd9\u4e9b\u5e93\u901a\u5e38\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong><code>pytz<\/code>\u5e93<\/strong><\/li>\n<\/ol>\n<p><p><code>pytz<\/code>\u5e93\u7528\u4e8e\u5904\u7406\u65f6\u533a\u8f6c\u6362\u3002\u5b83\u63d0\u4f9b\u4e86\u5173\u4e8e\u65f6\u533a\u7684\u4fe1\u606f\u5e76\u80fd\u8fdb\u884c\u65f6\u533a\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aUTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc_time = datetime.now(pytz.utc)<\/p>\n<p>print(f&quot;UTC\u65f6\u95f4: {utc_time}&quot;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u65f6\u533a<\/strong><\/h2>\n<p>eastern_time = utc_time.astimezone(pytz.timezone(&#39;US\/Eastern&#39;))<\/p>\n<p>print(f&quot;\u4e1c\u90e8\u65f6\u95f4: {eastern_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong><code>dateutil<\/code>\u5e93<\/strong><\/li>\n<\/ol>\n<p><p><code>dateutil<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u671f\u89e3\u6790\u529f\u80fd\uff0c\u7279\u522b\u662f\u5728\u89e3\u6790\u4eba\u7c7b\u53ef\u8bfb\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil import parser<\/p>\n<h2><strong>\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_string = &quot;October 1, 2023, 12:00 PM&quot;<\/p>\n<p>parsed_date = parser.parse(date_string)<\/p>\n<p>print(f&quot;\u89e3\u6790\u540e\u7684\u65e5\u671f: {parsed_date}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong><code>arrow<\/code>\u5e93<\/strong><\/li>\n<\/ol>\n<p><p><code>arrow<\/code>\u662f\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u7684\u65e5\u671f\u65f6\u95f4\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u5355\u7684API\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = arrow.now()<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4: {current_time}&quot;)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>later_time = current_time.shift(days=+1, hours=+2)<\/p>\n<p>time_difference = later_time - current_time<\/p>\n<p>print(f&quot;\u65f6\u95f4\u95f4\u9694: {time_difference}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u8ba1\u7b97\u6709\u8bb8\u591a\u5e94\u7528\u573a\u666f\uff0c\u6bd4\u5982\u4e8b\u4ef6\u8bb0\u5f55\u3001\u65e5\u5fd7\u5206\u6790\u3001\u5b9a\u65f6\u4efb\u52a1\u3001\u5012\u8ba1\u65f6\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e8b\u4ef6\u8bb0\u5f55<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4e8b\u4ef6\u8bb0\u5f55\u7cfb\u7edf\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u8bb0\u5f55\u4e8b\u4ef6\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u95f4\uff0c\u5e76\u8ba1\u7b97\u4e8b\u4ef6\u7684\u6301\u7eed\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u8bb0\u5f55\u4e8b\u4ef6\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = datetime.now()<\/p>\n<h2><strong>... \u4e8b\u4ef6\u8fdb\u884c\u4e2d ...<\/strong><\/h2>\n<h2><strong>\u8bb0\u5f55\u4e8b\u4ef6\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = datetime.now()<\/p>\n<h2><strong>\u8ba1\u7b97\u4e8b\u4ef6\u6301\u7eed\u65f6\u95f4<\/strong><\/h2>\n<p>duration = end_time - start_time<\/p>\n<p>print(f&quot;\u4e8b\u4ef6\u6301\u7eed\u65f6\u95f4: {duration}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u65e5\u5fd7\u5206\u6790<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u65e5\u5fd7\u5206\u6790\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u65e5\u5fd7\u8bb0\u5f55\u7684\u65f6\u95f4\u95f4\u9694\u6765\u5206\u6790\u7cfb\u7edf\u6027\u80fd\u548c\u54cd\u5e94\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u6a21\u62df\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p>log = [<\/p>\n<p>    &quot;2023-10-01 12:00:00&quot;,<\/p>\n<p>    &quot;2023-10-01 12:05:00&quot;,<\/p>\n<p>    &quot;2023-10-01 12:10:00&quot;<\/p>\n<p>]<\/p>\n<h2><strong>\u89e3\u6790\u65e5\u5fd7\u65f6\u95f4\u5e76\u8ba1\u7b97\u95f4\u9694<\/strong><\/h2>\n<p>for i in range(1, len(log)):<\/p>\n<p>    t1 = datetime.datetime.strptime(log[i-1], &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>    t2 = datetime.datetime.strptime(log[i], &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>    interval = t2 - t1<\/p>\n<p>    print(f&quot;\u65e5\u5fd7\u8bb0\u5f55\u95f4\u9694: {interval}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5b9a\u65f6\u4efb\u52a1<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u81ea\u52a8\u5316\u4efb\u52a1\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u6765\u8bbe\u5b9a\u4efb\u52a1\u7684\u6267\u884c\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from datetime import datetime, timedelta<\/p>\n<h2><strong>\u8bbe\u7f6e\u4efb\u52a1\u95f4\u9694<\/strong><\/h2>\n<p>interval = timedelta(seconds=10)<\/p>\n<h2><strong>\u5f00\u59cb\u4efb\u52a1\u5faa\u73af<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    # \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/p>\n<p>    current_time = datetime.now()<\/p>\n<p>    print(f&quot;\u6267\u884c\u4efb\u52a1: {current_time}&quot;)<\/p>\n<p>    # \u7b49\u5f85\u4e0b\u4e00\u4e2a\u95f4\u9694<\/p>\n<p>    time.sleep(interval.total_seconds())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5012\u8ba1\u65f6<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u5b9e\u73b0\u5012\u8ba1\u65f6\u529f\u80fd\uff0c\u6bd4\u5982\u63d0\u9192\u7528\u6237\u4efb\u52a1\u5373\u5c06\u5230\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from datetime import datetime, timedelta<\/p>\n<h2><strong>\u8bbe\u7f6e\u5012\u8ba1\u65f6<\/strong><\/h2>\n<p>countdown_time = timedelta(seconds=10)<\/p>\n<h2><strong>\u5f00\u59cb\u5012\u8ba1\u65f6<\/strong><\/h2>\n<p>end_time = datetime.now() + countdown_time<\/p>\n<p>while True:<\/p>\n<p>    # \u8ba1\u7b97\u5269\u4f59\u65f6\u95f4<\/p>\n<p>    rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ning_time = end_time - datetime.now()<\/p>\n<p>    if remaining_time.total_seconds() &lt;= 0:<\/p>\n<p>        print(&quot;\u5012\u8ba1\u65f6\u7ed3\u675f&quot;)<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;\u5269\u4f59\u65f6\u95f4: {remaining_time}&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8fd9\u4e9b\u4f8b\u5b50\uff0c\u53ef\u4ee5\u770b\u5230Python\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u529f\u80fd\u3002\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u9002\u5408\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\u901a\u8fc7\u5408\u7406\u5730\u5229\u7528Python\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f60\u53ef\u4ee5\u5728\u5404\u79cd\u9879\u76ee\u4e2d\u8f7b\u677e\u5904\u7406\u65f6\u95f4\u95f4\u9694\u8ba1\u7b97\u95ee\u9898\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\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u901a\u8fc7\u8c03\u7528<code>datetime.datetime.now()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5f97\u5230\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import datetime\ncurrent_time = datetime.datetime.now()\nprint(current_time)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5dee\u5f02\uff1f<\/strong><br \/>\u8981\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>timedelta<\/code>\u7c7b\u3002\u521b\u5efa\u4e24\u4e2a<code>datetime<\/code>\u5bf9\u8c61\u540e\uff0c\u7b80\u5355\u5730\u76f8\u51cf\u5373\u53ef\u5f97\u51fa\u65f6\u95f4\u95f4\u9694\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from datetime import datetime, timedelta\n\ntime1 = datetime(2023, 1, 1, 12, 0, 0)\ntime2 = datetime(2023, 1, 2, 14, 30, 0)\ntime_difference = time2 - time1\nprint(time_difference)  # \u8f93\u51fa\uff1a1 day, 2:30:00\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06\u65f6\u95f4\u95f4\u9694\u8f6c\u6362\u4e3a\u5c0f\u65f6\u548c\u5206\u949f\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee<code>timedelta<\/code>\u5bf9\u8c61\u7684\u5c5e\u6027\u6765\u83b7\u53d6\u603b\u5c0f\u65f6\u6570\u548c\u5206\u949f\u6570\u3002\u5c06<code>total_seconds()<\/code>\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u8fdb\u884c\u9002\u5f53\u8f6c\u6362\uff0c\u53ef\u4ee5\u5f97\u5230\u6240\u9700\u7684\u5355\u4f4d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">total_seconds = time_difference.total_seconds()\nhours = total_seconds \/\/ 3600\nminutes = (total_seconds % 3600) \/\/ 60\nprint(f&quot;\u65f6\u95f4\u95f4\u9694\u4e3a {int(hours)} \u5c0f\u65f6 {int(minutes)} \u5206\u949f&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u4f9d\u8d56\u4e8e\u6807\u51c6\u5e93\u4e2d\u7684datetime\u6a21\u5757\u3001time\u6a21\u5757\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982py [&hellip;]","protected":false},"author":3,"featured_media":1020470,"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\/1020463"}],"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=1020463"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020463\/revisions"}],"predecessor-version":[{"id":1020472,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020463\/revisions\/1020472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1020470"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1020463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1020463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1020463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}