{"id":1111973,"date":"2025-01-08T17:34:17","date_gmt":"2025-01-08T09:34:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111973.html"},"modified":"2025-01-08T17:34:20","modified_gmt":"2025-01-08T09:34:20","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%af%94%e8%be%83%e4%b8%a4%e4%b8%aa%e6%97%b6%e9%97%b4%e7%82%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111973.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074218\/a7caa7be-676c-4adb-920b-85b25a05d52b.webp\" alt=\"python\u4e2d\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u6765\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9\u3002<\/strong> \u901a\u8fc7datetime\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u6bd4\u8f83\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u3002\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528datetime\u5bf9\u8c61\u3001\u4f7f\u7528timedelta\u5bf9\u8c61\u3001\u5c06\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528datetime\u5bf9\u8c61\u6765\u8fdb\u884c\u65f6\u95f4\u70b9\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u63d0\u4f9b\u4e86datetime\u7c7b\uff0c\u53ef\u4ee5\u7528\u5b83\u6765\u521b\u5efa\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5bf9\u8c61\u3002\u901a\u8fc7\u6bd4\u8f83datetime\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5224\u65ad\u4e24\u4e2a\u65f6\u95f4\u70b9\u7684\u5148\u540e\u987a\u5e8f\u3002\u5177\u4f53\u65b9\u6cd5\u662f\uff0c\u901a\u8fc7\u5bfc\u5165datetime\u6a21\u5757\uff0c\u521b\u5efa\u4e24\u4e2a\u65f6\u95f4\u70b9\u7684datetime\u5bf9\u8c61\uff0c\u7136\u540e\u76f4\u63a5\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\u5982&lt;\u3001&gt;\u3001&lt;=\u3001&gt;=\u3001==\u3001!=\uff09\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165datetime\u6a21\u5757\u5e76\u521b\u5efadatetime\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165datetime\u6a21\u5757\uff0c\u5e76\u901a\u8fc7\u5b83\u521b\u5efadatetime\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u65f6\u95f4\u70b9<\/strong><\/h2>\n<p>time1 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>time2 = datetime(2023, 10, 1, 15, 30, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u597ddatetime\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u6765\u6bd4\u8f83\u8fd9\u4e24\u4e2a\u65f6\u95f4\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9<\/p>\n<p>if time1 &lt; time2:<\/p>\n<p>    print(&quot;time1 \u65e9\u4e8e time2&quot;)<\/p>\n<p>elif time1 &gt; time2:<\/p>\n<p>    print(&quot;time1 \u665a\u4e8e time2&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;time1 \u4e0e time2 \u76f8\u540c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u5e76\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u5f80\u5f80\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u7136\u540e\u8fdb\u884c\u6bd4\u8f83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/strong><\/h2>\n<p>time_str1 = &#39;2023-10-01 12:00:00&#39;<\/p>\n<p>time_str2 = &#39;2023-10-01 15:30:00&#39;<\/p>\n<p>time1 = datetime.strptime(time_str1, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>time2 = datetime.strptime(time_str2, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<h2><strong>\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9<\/strong><\/h2>\n<p>if time1 &lt; time2:<\/p>\n<p>    print(&quot;time1 \u65e9\u4e8e time2&quot;)<\/p>\n<p>elif time1 &gt; time2:<\/p>\n<p>    print(&quot;time1 \u665a\u4e8e time2&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;time1 \u4e0e time2 \u76f8\u540c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528timedelta\u5bf9\u8c61\u8fdb\u884c\u65f6\u95f4\u5dee\u7684\u8ba1\u7b97\u548c\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u8fd8\u9700\u8981\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u65f6\u95f4\u5dee\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528timedelta\u5bf9\u8c61\u3002timedelta\u5bf9\u8c61\u8868\u793a\u4e24\u4e2a\u65e5\u671f\u65f6\u95f4\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime, timedelta<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u65f6\u95f4\u70b9<\/strong><\/h2>\n<p>time1 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>time2 = datetime(2023, 10, 1, 15, 30, 0)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>time_difference = time2 - time1<\/p>\n<h2><strong>\u8f93\u51fa\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>print(&quot;\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5dee\u5f02:&quot;, time_difference)<\/p>\n<h2><strong>\u6bd4\u8f83\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>if time_difference &gt; timedelta(hours=2):<\/p>\n<p>    print(&quot;\u65f6\u95f4\u5dee\u5927\u4e8e2\u5c0f\u65f6&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u65f6\u95f4\u5dee\u5c0f\u4e8e\u6216\u7b49\u4e8e2\u5c0f\u65f6&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u65f6\u95f4\u65f6\uff0c\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9\uff0c\u5e76\u6839\u636e\u7ed3\u679c\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>def compare_user_times(time_str1, time_str2):<\/p>\n<p>    # \u5c06\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/p>\n<p>    time1 = datetime.strptime(time_str1, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>    time2 = datetime.strptime(time_str2, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>    # \u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9<\/p>\n<p>    if time1 &lt; time2:<\/p>\n<p>        return &quot;\u7b2c\u4e00\u4e2a\u65f6\u95f4\u70b9\u65e9\u4e8e\u7b2c\u4e8c\u4e2a\u65f6\u95f4\u70b9&quot;<\/p>\n<p>    elif time1 &gt; time2:<\/p>\n<p>        return &quot;\u7b2c\u4e00\u4e2a\u65f6\u95f4\u70b9\u665a\u4e8e\u7b2c\u4e8c\u4e2a\u65f6\u95f4\u70b9&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;\u4e24\u4e2a\u65f6\u95f4\u70b9\u76f8\u540c&quot;<\/p>\n<h2><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong><\/h2>\n<p>user_time1 = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u65f6\u95f4\u70b9 (\u683c\u5f0f: YYYY-MM-DD HH:MM:SS): &quot;)<\/p>\n<p>user_time2 = input(&quot;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u65f6\u95f4\u70b9 (\u683c\u5f0f: YYYY-MM-DD HH:MM:SS): &quot;)<\/p>\n<h2><strong>\u6bd4\u8f83\u5e76\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>result = compare_user_times(user_time1, user_time2)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u4e0d\u540c\u7684\u65f6\u533a<\/h3>\n<\/p>\n<p><p>\u5728\u6bd4\u8f83\u4e0d\u540c\u65f6\u95f4\u70b9\u65f6\uff0c\u65f6\u533a\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002datetime\u6a21\u5757\u4e2d\u7684<code>timezone<\/code>\u7c7b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u65f6\u533a\u95ee\u9898\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u5e26\u6709\u65f6\u533a\u7684\u65f6\u95f4\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime, timezone, timedelta<\/p>\n<h2><strong>\u521b\u5efa\u5e26\u6709\u65f6\u533a\u7684datetime\u5bf9\u8c61<\/strong><\/h2>\n<p>time1 = datetime(2023, 10, 1, 12, 0, 0, tzinfo=timezone.utc)<\/p>\n<p>time2 = datetime(2023, 10, 1, 15, 0, 0, tzinfo=timezone(timedelta(hours=3)))<\/p>\n<h2><strong>\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9<\/strong><\/h2>\n<p>if time1 &lt; time2:<\/p>\n<p>    print(&quot;time1 \u65e9\u4e8e time2&quot;)<\/p>\n<p>elif time1 &gt; time2:<\/p>\n<p>    print(&quot;time1 \u665a\u4e8e time2&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;time1 \u4e0e time2 \u76f8\u540c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728Python\u4e2d\u4f7f\u7528datetime\u6a21\u5757\u6765\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9\u662f\u975e\u5e38\u65b9\u4fbf\u7684\u3002<strong>\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165datetime\u6a21\u5757\u3001\u521b\u5efadatetime\u5bf9\u8c61\u3001\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\u3001\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u3001\u4f7f\u7528timedelta\u5bf9\u8c61\u8fdb\u884c\u65f6\u95f4\u5dee\u7684\u8ba1\u7b97\u548c\u6bd4\u8f83<\/strong>\u3002\u5728\u5904\u7406\u65f6\u533a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>timezone<\/code>\u7c7b\u6765\u521b\u5efa\u5e26\u6709\u65f6\u533a\u7684datetime\u5bf9\u8c61\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5b9e\u6218\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u65f6\u95f4\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u548c\u6bd4\u8f83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u6218\u6280\u5de7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406\u65f6\u95f4\u6bd4\u8f83\u95ee\u9898\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65f6\u95f4<\/strong>\uff1a\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65f6\u95f4\u65f6\uff0c\u8981\u8003\u8651\u5230\u7528\u6237\u53ef\u80fd\u8f93\u5165\u4e0d\u6b63\u786e\u7684\u683c\u5f0f\uff0c\u4f7f\u7528try-except\u5757\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002<\/li>\n<li><strong>\u65f6\u95f4\u683c\u5f0f\u7684\u7edf\u4e00<\/strong>\uff1a\u5728\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83\u4e4b\u524d\uff0c\u786e\u4fdd\u6240\u6709\u65f6\u95f4\u70b9\u7684\u683c\u5f0f\u662f\u7edf\u4e00\u7684\uff0c\u907f\u514d\u56e0\u4e3a\u683c\u5f0f\u4e0d\u540c\u5bfc\u81f4\u6bd4\u8f83\u7ed3\u679c\u4e0d\u51c6\u786e\u3002<\/li>\n<li><strong>\u8003\u8651\u65f6\u95f4\u7684\u7cbe\u5ea6<\/strong>\uff1a\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65f6\u95f4\u7cbe\u5ea6\uff08\u5982\u79d2\u3001\u5206\u949f\u3001\u5c0f\u65f6\uff09\uff0c\u907f\u514d\u56e0\u4e3a\u7cbe\u5ea6\u95ee\u9898\u5bfc\u81f4\u6bd4\u8f83\u7ed3\u679c\u4e0d\u51c6\u786e\u3002<\/li>\n<li><strong>\u4f7f\u7528\u65f6\u95f4\u5e93<\/strong>\uff1a\u5728\u5904\u7406\u590d\u6742\u7684\u65f6\u95f4\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u65f6\u95f4\u5e93\uff0c\u5982<code>pytz<\/code>\u3001<code>dateutil<\/code>\u7b49\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u65f6\u533a\u652f\u6301\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6280\u5de7\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u548c\u6bd4\u8f83\u65f6\u95f4\u70b9\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u548c\u53ef\u9760\u6027\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\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\u5e76\u4e0e\u5176\u4ed6\u65f6\u95f4\u8fdb\u884c\u6bd4\u8f83\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u901a\u8fc7<code>datetime.datetime.now()<\/code>\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u8981\u4e0e\u5176\u4ed6\u65f6\u95f4\u8fdb\u884c\u6bd4\u8f83\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u65f6\u95f4\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\u5982<code>&lt;<\/code>\u3001<code>&gt;<\/code>\u3001<code>==<\/code>\u7b49\uff09\u8fdb\u884c\u6bd4\u8f83\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\n\ncurrent_time = datetime.datetime.now()\nother_time = datetime.datetime(2023, 10, 1, 12, 0)  # \u8bbe\u5b9a\u4e00\u4e2a\u5176\u4ed6\u65f6\u95f4\n\nif current_time &gt; other_time:\n    print(&quot;\u5f53\u524d\u65f6\u95f4\u665a\u4e8e\u5176\u4ed6\u65f6\u95f4&quot;)\nelse:\n    print(&quot;\u5f53\u524d\u65f6\u95f4\u65e9\u4e8e\u6216\u7b49\u4e8e\u5176\u4ed6\u65f6\u95f4&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e0d\u540c\u65f6\u533a\u7684\u65f6\u95f4\u6bd4\u8f83\uff1f<\/strong><br \/>\u5904\u7406\u4e0d\u540c\u65f6\u533a\u7684\u65f6\u95f4\u6bd4\u8f83\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u6765\u5904\u7406\u65f6\u533a\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5<code>pytz<\/code>\u5e93\u3002\u901a\u8fc7\u5b9a\u4e49\u65f6\u95f4\u7684\u65f6\u533a\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u65f6\u95f4\u6bd4\u8f83\u7684\u51c6\u786e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\nimport pytz\n\nutc_time = datetime.datetime.now(pytz.utc)\nlocal_tz = pytz.timezone(&#39;Asia\/Shangh<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>&#39;)\nlocal_time = utc_time.astimezone(local_tz)\n\nprint(&quot;UTC\u65f6\u95f4:&quot;, utc_time)\nprint(&quot;\u672c\u5730\u65f6\u95f4:&quot;, local_time)\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u6bd4\u8f83\u4e0d\u540c\u65f6\u95f4\u533a\u57df\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\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\u7684<code>timedelta<\/code>\u5bf9\u8c61\u3002\u901a\u8fc7\u7b80\u5355\u7684\u51cf\u6cd5\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5f97\u5230\u65f6\u95f4\u5dee\u3002\u65f6\u95f4\u5dee\u4f1a\u4ee5\u5929\u3001\u79d2\u548c\u5fae\u79d2\u7684\u5f62\u5f0f\u8868\u793a\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\n\nstart_time = datetime.datetime(2023, 10, 1, 12, 0)\nend_time = datetime.datetime(2023, 10, 5, 15, 30)\n\ntime_difference = end_time - start_time\nprint(&quot;\u65f6\u95f4\u5dee:&quot;, time_difference)  # \u8f93\u51fa: \u65f6\u95f4\u5dee: 4 days, 3:30:00\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6e05\u6670\u5730\u4e86\u89e3\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5177\u4f53\u5dee\u5f02\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u6765\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u70b9\u3002 \u901a\u8fc7datetime\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa [&hellip;]","protected":false},"author":3,"featured_media":1111983,"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\/1111973"}],"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=1111973"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111973\/revisions"}],"predecessor-version":[{"id":1111984,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111973\/revisions\/1111984"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111983"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}