{"id":1097122,"date":"2025-01-08T15:08:36","date_gmt":"2025-01-08T07:08:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097122.html"},"modified":"2025-01-08T15:08:39","modified_gmt":"2025-01-08T07:08:39","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e4%b8%a4%e4%b8%aa%e6%97%b6%e9%97%b4%e7%9b%b8%e5%87%8f-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097122.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u4e24\u4e2a\u65f6\u95f4\u76f8\u51cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212024\/56163cf3-7f26-404c-b705-ee4ada149d34.webp\" alt=\"python\u5982\u4f55\u8ba9\u4e24\u4e2a\u65f6\u95f4\u76f8\u51cf\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8fdb\u884c\u65f6\u95f4\u76f8\u51cf\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001\u51cf\u53bb\u4e24\u4e2adatetime\u5bf9\u8c61\u3001\u4f7f\u7528timedelta\u5bf9\u8c61\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u501f\u52a9datetime\u6a21\u5757\u4e2d\u7684datetime\u548ctimedelta\u7c7b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u65f6\u95f4\u548c\u65e5\u671f\u7684\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7datetime\u6a21\u5757\u6765\u5b8c\u6210\uff0c\u7279\u522b\u662fdatetime\u7c7b\u548ctimedelta\u7c7b\u3002\u901a\u8fc7\u521b\u5efadatetime\u5bf9\u8c61\u6765\u8868\u793a\u5177\u4f53\u7684\u65f6\u95f4\u70b9\uff0c\u7136\u540e\u4f7f\u7528\u51cf\u6cd5\u64cd\u4f5c\u7b26\u6765\u8ba1\u7b97\u65f6\u95f4\u5dee\uff0c\u8fd4\u56de\u4e00\u4e2atimedelta\u5bf9\u8c61\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528datetime\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u4e4b\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efadatetime\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165datetime\u6a21\u5757\u5e76\u521b\u5efadatetime\u5bf9\u8c61\u3002datetime\u5bf9\u8c61\u53ef\u4ee5\u8868\u793a\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2adatetime\u5bf9\u8c61<\/strong><\/h2>\n<p>time1 = datetime(2023, 10, 10, 15, 30, 0)<\/p>\n<p>time2 = datetime(2023, 10, 12, 18, 45, 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u65f6\u95f4\u5dee<\/h4>\n<\/p>\n<p><p>\u51cf\u53bb\u4e24\u4e2adatetime\u5bf9\u8c61\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2atimedelta\u5bf9\u8c61\uff0c\u8868\u793a\u65f6\u95f4\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u65f6\u95f4\u5dee<\/p>\n<p>time_difference = time2 - time1<\/p>\n<p>print(&quot;\u65f6\u95f4\u5dee:&quot;, time_difference)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001timedelta\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>timedelta\u5bf9\u8c61\u8868\u793a\u65f6\u95f4\u95f4\u9694\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u65f6\u95f4\u5dee\u7684\u8ba1\u7b97\u548c\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u65f6\u95f4\u5dee\u7684\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>timedelta\u5bf9\u8c61\u6709\u51e0\u4e2a\u5e38\u7528\u5c5e\u6027\uff0c\u4f8b\u5982\u5929\u6570\u3001\u79d2\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u65f6\u95f4\u5dee\u7684\u5c5e\u6027<\/p>\n<p>days = time_difference.days<\/p>\n<p>seconds = time_difference.seconds<\/p>\n<p>total_seconds = time_difference.total_seconds()<\/p>\n<p>print(&quot;\u5929\u6570:&quot;, days)<\/p>\n<p>print(&quot;\u79d2\u6570:&quot;, seconds)<\/p>\n<p>print(&quot;\u603b\u79d2\u6570:&quot;, total_seconds)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8f6c\u6362\u65f6\u95f4\u5dee\u4e3a\u5176\u4ed6\u5355\u4f4d<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u65f6\u95f4\u5dee\u8f6c\u6362\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u7b49\u5355\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u65f6\u95f4\u5dee\u4e3a\u5c0f\u65f6\u548c\u5206\u949f<\/p>\n<p>hours = total_seconds \/\/ 3600<\/p>\n<p>minutes = (total_seconds % 3600) \/\/ 60<\/p>\n<p>print(&quot;\u5c0f\u65f6:&quot;, hours)<\/p>\n<p>print(&quot;\u5206\u949f:&quot;, minutes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u65f6\u533a\u5dee\u5f02<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u65f6\u533a\u5dee\u5f02\uff0c\u53ef\u4ee5\u4f7f\u7528pytz\u6a21\u5757\u3002pytz\u6a21\u5757\u63d0\u4f9b\u4e86\u65f6\u533a\u652f\u6301\uff0c\u53ef\u4ee5\u5c06datetime\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u65f6\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pytz<\/p>\n<h2><strong>\u521b\u5efa\u5e26\u65f6\u533a\u7684datetime\u5bf9\u8c61<\/strong><\/h2>\n<p>tz = pytz.timezone(&#39;Asia\/Shangh<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>&#39;)<\/p>\n<p>time1 = tz.localize(datetime(2023, 10, 10, 15, 30, 0))<\/p>\n<p>time2 = tz.localize(datetime(2023, 10, 12, 18, 45, 0))<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>time_difference = time2 - time1<\/p>\n<p>print(&quot;\u65f6\u95f4\u5dee:&quot;, time_difference)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u5b57\u7b26\u4e32\u89e3\u6790\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u89e3\u6790\u65f6\u95f4\uff0cdatetime\u6a21\u5757\u63d0\u4f9b\u4e86strptime\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u89e3\u6790datetime\u5bf9\u8c61<\/strong><\/h2>\n<p>time1 = datetime.strptime(&#39;2023-10-10 15:30:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>time2 = datetime.strptime(&#39;2023-10-12 18:45:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>time_difference = time2 - time1<\/p>\n<p>print(&quot;\u65f6\u95f4\u5dee:&quot;, time_difference)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5c06\u65f6\u95f4\u5dee\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06timedelta\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fbf\u4e8e\u663e\u793a\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u683c\u5f0f\u5316\u65f6\u95f4\u5dee<\/p>\n<p>formatted_difference = str(time_difference)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u7684\u65f6\u95f4\u5dee:&quot;, formatted_difference)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u793a\u4f8b\uff0c\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u5e76\u4ee5\u5929\u6570\u3001\u5c0f\u65f6\u3001\u5206\u949f\u7b49\u5f62\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u89e3\u6790datetime\u5bf9\u8c61<\/strong><\/h2>\n<p>start_time = datetime.strptime(&#39;2023-10-10 08:00:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>end_time = datetime.strptime(&#39;2023-10-12 18:30:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>time_difference = end_time - start_time<\/p>\n<h2><strong>\u83b7\u53d6\u65f6\u95f4\u5dee\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>days = time_difference.days<\/p>\n<p>total_seconds = time_difference.total_seconds()<\/p>\n<p>hours = total_seconds \/\/ 3600<\/p>\n<p>minutes = (total_seconds % 3600) \/\/ 60<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&quot;\u65f6\u95f4\u5dee: {days} \u5929, {hours} \u5c0f\u65f6, {minutes} \u5206\u949f&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u65b9\u4fbf\u5730\u8fdb\u884c\u65f6\u95f4\u76f8\u51cf\u64cd\u4f5c\uff0c\u5e76\u6839\u636e\u9700\u8981\u5904\u7406\u548c\u683c\u5f0f\u5316\u65f6\u95f4\u5dee\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u65f6\u95f4\u548c\u65e5\u671f\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u65f6\u95f4\u5bf9\u8c61\u4ee5\u4fbf\u8fdb\u884c\u65f6\u95f4\u76f8\u51cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u521b\u5efa\u65f6\u95f4\u5bf9\u8c61\u3002\u901a\u8fc7<code>datetime.datetime<\/code>\u7c7b\uff0c\u53ef\u4ee5\u521b\u5efa\u7279\u5b9a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4f7f\u7528<code>datetime.strptime()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c<code>datetime.strptime(&#39;2023-10-10 10:00:00&#39;, &#39;%Y-%m-%d %H:%M:%S&#39;)<\/code>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u5bf9\u8c61\u3002\u521b\u5efa\u4e24\u4e2a\u65f6\u95f4\u5bf9\u8c61\u540e\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u76f8\u51cf\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u76f8\u51cf\u540e\u7684\u65f6\u95f4\u5dee\u5982\u4f55\u8868\u793a\u548c\u83b7\u53d6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4e24\u4e2a\u65f6\u95f4\u5bf9\u8c61\u76f8\u51cf\u4f1a\u8fd4\u56de\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\u3002\u8fd9\u4e2a\u5bf9\u8c61\u5305\u542b\u4e86\u76f8\u51cf\u540e\u7684\u65f6\u95f4\u5dee\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>days<\/code>, <code>seconds<\/code>, \u548c <code>microseconds<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u5177\u4f53\u7684\u65f6\u95f4\u5dee\u3002\u4f8b\u5982\uff0c<code>time_difference.days<\/code>\u4f1a\u8fd4\u56de\u76f8\u5dee\u7684\u5929\u6570\uff0c<code>time_difference.seconds<\/code>\u4f1a\u8fd4\u56de\u76f8\u5dee\u7684\u79d2\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5904\u7406\u65f6\u533a\u4e0d\u540c\u7684\u65f6\u95f4\u76f8\u51cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406\u65f6\u533a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u6216\u8005<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>timezone<\/code>\u7c7b\u3002\u786e\u4fdd\u4e24\u4e2a\u65f6\u95f4\u5bf9\u8c61\u90fd\u662f\u5728\u540c\u4e00\u65f6\u533a\u4e0b\u518d\u8fdb\u884c\u76f8\u51cf\u3002\u53ef\u4ee5\u901a\u8fc7<code>localize()<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a\u65f6\u533a\uff0c\u6216\u4f7f\u7528<code>astimezone()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u53e6\u4e00\u65f6\u533a\u3002\u8fd9\u786e\u4fdd\u4e86\u76f8\u51cf\u64cd\u4f5c\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8fdb\u884c\u65f6\u95f4\u76f8\u51cf\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001\u51cf\u53bb\u4e24\u4e2adatetime\u5bf9\u8c61\u3001\u4f7f\u7528time [&hellip;]","protected":false},"author":3,"featured_media":1097133,"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\/1097122"}],"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=1097122"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097122\/revisions"}],"predecessor-version":[{"id":1097134,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097122\/revisions\/1097134"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097133"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}