{"id":1053058,"date":"2024-12-31T14:30:37","date_gmt":"2024-12-31T06:30:37","guid":{"rendered":""},"modified":"2024-12-31T14:30:40","modified_gmt":"2024-12-31T06:30:40","slug":"python%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%9b%b8%e7%ad%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1053058.html","title":{"rendered":"Python\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4a2c4b3e-8d0a-42e5-b667-aae171ea13e9.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528\u65f6\u95f4\u6233\u3001\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u6bd4\u8f83\u3002\u5176\u4e2d\uff0c\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3adatetime\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\uff0c\u5e76\u4e14\u80fd\u591f\u5904\u7406\u65f6\u533a\u7b49\u590d\u6742\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528datetime\u6a21\u5757\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165datetime\u6a21\u5757\u3002<\/li>\n<li>\u521b\u5efa\u4e24\u4e2adatetime\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08==\u3001!=\u3001&lt;\u3001&gt;\u3001&lt;=\u3001&gt;=\uff09\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>time1 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>time2 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>if time1 == time2:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>time1<\/code>\u548c<code>time2<\/code>\u662f\u4e24\u4e2a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u901a\u8fc7\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u6bd4\u8f83\u5b83\u4eec\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528datetime\u6a21\u5757\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\uff0c\u4f7f\u5f97\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u662f\u5426\u76f8\u7b49\u53d8\u5f97\u975e\u5e38\u5bb9\u6613\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165datetime\u6a21\u5757\uff0c\u7136\u540e\u53ef\u4ee5\u521b\u5efa\u4e24\u4e2adatetime\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efadatetime\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>time1 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>time2 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u5206\u522b\u8868\u793a2023\u5e7410\u67081\u65e5\u4e2d\u534812\u70b9\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6bd4\u8f83datetime\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if time1 == time2:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u6bd4\u8f83\u4e24\u4e2adatetime\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b8c\u5168\u4e00\u81f4\uff0c\u90a3\u4e48\u5b83\u4eec\u88ab\u8ba4\u4e3a\u662f\u76f8\u7b49\u7684\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u65f6\u95f4\u6233\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u6bd4\u8f83\u65f6\u95f4\u7684\u65b9\u6cd5\u662f\u5c06datetime\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\uff0c\u7136\u540e\u8fdb\u884c\u6bd4\u8f83\u3002\u65f6\u95f4\u6233\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u81ea1970\u5e741\u67081\u65e500:00:00 UTC\u4ee5\u6765\u7684\u79d2\u6570\u3002\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u4e2d\u7684<code>timestamp()<\/code>\u65b9\u6cd5\u5c06datetime\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u65f6\u95f4\u6233<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">timestamp1 = time1.timestamp()<\/p>\n<p>timestamp2 = time2.timestamp()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>timestamp()<\/code>\u65b9\u6cd5\u5c06\u4e24\u4e2a<code>datetime<\/code>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6bd4\u8f83\u65f6\u95f4\u6233<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if timestamp1 == timestamp2:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u65f6\u95f4\u6233\u7684\u6bd4\u8f83\u548cdatetime\u5bf9\u8c61\u7684\u6bd4\u8f83\u7c7b\u4f3c\uff0c\u540c\u6837\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u3002\u5982\u679c\u4e24\u4e2a\u65f6\u95f4\u6233\u76f8\u7b49\uff0c\u5219\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528time\u6a21\u5757\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684time\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u65f6\u95f4\u3002time\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u65f6\u95f4\u7684\u5404\u79cd\u51fd\u6570\u548c\u5e38\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>time.struct_time<\/code>\u5bf9\u8c61\u548c\u65f6\u95f4\u6233\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efastruct_time\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>time1_struct = time.strptime(&#39;2023-10-01 12:00:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>time2_struct = time.strptime(&#39;2023-10-01 12:00:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>strptime()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a<code>time.struct_time<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6bd4\u8f83struct_time\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if time1_struct == time2_struct:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>time.struct_time<\/code>\u5bf9\u8c61\u7684\u6bd4\u8f83\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u3002\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u5b57\u6bb5\u90fd\u76f8\u7b49\uff0c\u5219\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u6bd4\u8f83\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8fdb\u884c\u5b57\u7b26\u4e32\u6bd4\u8f83\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u65f6\u95f4\u683c\u5f0f\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">time1_str = &#39;2023-10-01 12:00:00&#39;<\/p>\n<p>time2_str = &#39;2023-10-01 12:00:00&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u8868\u793a\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6bd4\u8f83\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if time1_str == time2_str:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u5b8c\u5168\u76f8\u540c\uff0c\u5219\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u662f\u5426\u76f8\u7b49\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u65f6\u95f4\u6233\u3001time\u6a21\u5757\u548c\u5b57\u7b26\u4e32\u6bd4\u8f83\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528datetime\u6a21\u5757\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efadatetime\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>time1 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>time2 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528datetime\u6a21\u5757\u521b\u5efa\u4e86\u4e24\u4e2adatetime\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6bd4\u8f83datetime\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if time1 == time2:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u6bd4\u8f83\u4e24\u4e2adatetime\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528datetime\u6a21\u5757\u7684\u9ad8\u7ea7\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u4e0d\u4ec5\u53ef\u4ee5\u6bd4\u8f83\u76f8\u7b49\u6027\uff0c\u8fd8\u53ef\u4ee5\u6bd4\u8f83\u5927\u5c0f\u3002\u8fd9\u5728\u5904\u7406\u65f6\u95f4\u8303\u56f4\u548c\u4e8b\u4ef6\u6392\u5e8f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6bd4\u8f83\u5927\u5c0f<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>time1 = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>time2 = datetime(2023, 10, 1, 15, 0, 0)<\/p>\n<p>if time1 &lt; time2:<\/p>\n<p>    print(&quot;time1 is earlier than time2&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;time1 is later than or equal to time2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>&lt;<\/code>\u8fd0\u7b97\u7b26\u6bd4\u8f83\u4e24\u4e2adatetime\u5bf9\u8c61\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">time_difference = time2 - time1<\/p>\n<p>print(f&quot;Time difference: {time_difference}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u51cf\u6cd5\u8fd0\u7b97\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u4e24\u4e2adatetime\u5bf9\u8c61\u4e4b\u95f4\u7684\u65f6\u95f4\u5dee\u3002<\/p>\n<\/p>\n<p><p><strong>\u5904\u7406\u65f6\u533a\u7684\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8de8\u65f6\u533a\u7684\u65f6\u95f4\u6bd4\u8f83\u65f6\uff0cdatetime\u6a21\u5757\u7684timezone\u529f\u80fd\u975e\u5e38\u6709\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pytz\u5e93\u6765\u5904\u7406\u65f6\u533a\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5pytz\u5e93<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pytz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u5e26\u65f6\u533a\u7684datetime\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<p>timezone_utc = pytz.utc<\/p>\n<p>timezone_est = pytz.timezone(&#39;US\/Eastern&#39;)<\/p>\n<p>time1 = datetime(2023, 10, 1, 12, 0, 0, tzinfo=timezone_utc)<\/p>\n<p>time2 = datetime(2023, 10, 1, 12, 0, 0, tzinfo=timezone_est)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u5e26\u6709\u65f6\u533a\u4fe1\u606f\u7684datetime\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6bd4\u8f83\u5e26\u65f6\u533a\u7684datetime\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if time1 == time2:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5e26\u6709\u65f6\u533a\u4fe1\u606f\u7684datetime\u5bf9\u8c61\u5728\u6bd4\u8f83\u65f6\u4f1a\u8003\u8651\u65f6\u533a\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u65f6\u95f4\u6233\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u65f6\u95f4\u6233\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u81ea1970\u5e741\u67081\u65e500:00:00 UTC\u4ee5\u6765\u7684\u79d2\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u65f6\u95f4\u6233\u6765\u6bd4\u8f83\u65f6\u95f4\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u65f6\u95f4\u6233<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">timestamp1 = time1.timestamp()<\/p>\n<p>timestamp2 = time2.timestamp()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528datetime\u5bf9\u8c61\u7684timestamp()\u65b9\u6cd5\u521b\u5efa\u4e86\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6bd4\u8f83\u65f6\u95f4\u6233<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if timestamp1 == timestamp2:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u65f6\u95f4\u6233\u7684\u6bd4\u8f83\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\uff0c\u5982\u679c\u4e24\u4e2a\u65f6\u95f4\u6233\u76f8\u7b49\uff0c\u5219\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528time\u6a21\u5757\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>time\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u65f6\u95f4\u7684\u5404\u79cd\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u65f6\u95f4\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efastruct_time\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>time1_struct = time.strptime(&#39;2023-10-01 12:00:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>time2_struct = time.strptime(&#39;2023-10-01 12:00:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528strptime()\u65b9\u6cd5\u5c06\u65f6\u95f4\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3atime.struct_time\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6bd4\u8f83struct_time\u5bf9\u8c61<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if time1_struct == time2_struct:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>time.struct_time<\/code>\u5bf9\u8c61\u7684\u6bd4\u8f83\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\uff0c\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u5b57\u6bb5\u90fd\u76f8\u7b49\uff0c\u5219\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u6bd4\u8f83\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u65f6\u95f4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u8f83\u662f\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u65f6\u95f4\u683c\u5f0f\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">time1_str = &#39;2023-10-01 12:00:00&#39;<\/p>\n<p>time2_str = &#39;2023-10-01 12:00:00&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u8868\u793a\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6bd4\u8f83\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">if time1_str == time2_str:<\/p>\n<p>    print(&quot;The two times are equal&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The two times are not equal&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u5b8c\u5168\u76f8\u540c\uff0c\u5219\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u662f\u5426\u76f8\u7b49\uff0c<strong>\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u65f6\u95f4\u6233\u3001time\u6a21\u5757\u548c\u5b57\u7b26\u4e32\u6bd4\u8f83\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e24\u4e2a\u65f6\u95f4\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u5904\u7406\u65f6\u95f4\u5bf9\u8c61\u3002\u8981\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u662f\u5426\u76f8\u7b49\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e24\u4e2a\u65f6\u95f4\u5bf9\u8c61\u5e76\u6bd4\u8f83\u5b83\u4eec\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ntime1 = datetime(2023, 10, 1, 12, 30)\ntime2 = datetime(2023, 10, 1, 12, 30)\n\nif time1 == time2:\n    print(&quot;\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49&quot;)\nelse:\n    print(&quot;\u4e24\u4e2a\u65f6\u95f4\u4e0d\u76f8\u7b49&quot;)\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u7528\u4ec0\u4e48\u65b9\u5f0f\u6bd4\u8f83\u65f6\u95f4\u7684\u4e0d\u540c\u90e8\u5206\uff1f<\/strong><br \/>\u9664\u4e86\u76f4\u63a5\u6bd4\u8f83\u65f6\u95f4\u5bf9\u8c61\u7684\u76f8\u7b49\u6027\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u6bd4\u8f83\u65f6\u95f4\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u4f8b\u5982\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u53ef\u4ee5\u4f7f\u7528<code>hour<\/code>\u3001<code>minute<\/code>\u548c<code>second<\/code>\u5c5e\u6027\u6765\u5206\u522b\u8bbf\u95ee\u8fd9\u4e9b\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">if time1.hour == time2.hour and time1.minute == time2.minute:\n    print(&quot;\u65f6\u95f4\u7684\u5c0f\u65f6\u548c\u5206\u949f\u76f8\u7b49&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0d\u540c\u65f6\u95f4\u683c\u5f0f\u7684\u6bd4\u8f83\uff1f<\/strong><br \/>\u5728\u6bd4\u8f83\u4e0d\u540c\u683c\u5f0f\u7684\u65f6\u95f4\u65f6\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5b83\u4eec\u90fd\u88ab\u8f6c\u6362\u4e3a\u76f8\u540c\u7684\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e24\u4e2a\u4e0d\u540c\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8fd9\u6837\u5904\u7406\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ntime_str1 = &quot;2023-10-01 12:30:00&quot;\ntime_str2 = &quot;01\/10\/2023 12:30:00&quot;\n\ntime1 = datetime.strptime(time_str1, &quot;%Y-%m-%d %H:%M:%S&quot;)\ntime2 = datetime.strptime(time_str2, &quot;%d\/%m\/%Y %H:%M:%S&quot;)\n\nif time1 == time2:\n    print(&quot;\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49&quot;)\nelse:\n    print(&quot;\u4e24\u4e2a\u65f6\u95f4\u4e0d\u76f8\u7b49&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u6bd4\u8f83\u662f\u51c6\u786e\u7684\uff0c\u65e0\u8bba\u8f93\u5165\u7684\u683c\u5f0f\u5982\u4f55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u76f8\u7b49\uff1a\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528\u65f6\u95f4\u6233\u3001\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u6bd4\u8f83\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1053066,"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\/1053058"}],"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=1053058"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053058\/revisions"}],"predecessor-version":[{"id":1053068,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053058\/revisions\/1053068"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053066"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}