{"id":1023280,"date":"2024-12-30T14:03:41","date_gmt":"2024-12-30T06:03:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1023280.html"},"modified":"2024-12-30T14:03:43","modified_gmt":"2024-12-30T06:03:43","slug":"python%e4%b8%ad%e7%ae%ad%e5%a4%b4%e5%a6%82%e4%bd%95%e5%85%91%e6%8d%a2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1023280.html","title":{"rendered":"python\u4e2d\u7bad\u5934\u5982\u4f55\u5151\u6362"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5a767d6b-73dd-4a1f-bbc8-07d0fa0fabc8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u7bad\u5934\u5982\u4f55\u5151\u6362\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7bad\u5934\uff08Arrow\uff09\u5982\u4f55\u5151\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c<strong>\u7bad\u5934\uff08Arrow\uff09\u5e93<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u5de5\u5177\u3002\u5176\u4e3b\u8981\u529f\u80fd\u5305\u62ec\uff1a<strong>\u89e3\u6790\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u3001\u65f6\u533a\u8f6c\u6362<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528Arrow\u5e93\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p><strong>\u89e3\u6790\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong>\uff1aArrow\u5e93\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u521b\u5efa\u4e00\u4e2aArrow\u5bf9\u8c61\u5e76\u81ea\u52a8\u8bc6\u522b\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61<\/strong>\uff1aArrow\u5141\u8bb8\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u901a\u8fc7\u6307\u5b9a\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u65f6\u533a\u8f6c\u6362<\/strong>\uff1aArrow\u5e93\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u65f6\u533a\u8f6c\u6362\u3002\u53ef\u4ee5\u5c06\u4e00\u4e2aArrow\u5bf9\u8c61\u4ece\u4e00\u4e2a\u65f6\u533a\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u65f6\u533a\uff0c\u5e76\u786e\u4fdd\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u89e3\u6790\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>Arrow\u5e93\u4f7f\u5f97\u89e3\u6790\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u8981\u8c03\u7528<code>arrow.get<\/code>\u65b9\u6cd5\uff0c\u5e76\u4f20\u5165\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<h2><strong>\u89e3\u6790\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &quot;2023-11-21T15:30:00&quot;<\/p>\n<p>date_obj = arrow.get(date_str)<\/p>\n<p>print(date_obj)  # 2023-11-21T15:30:00+00:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u89e3\u6790\u4e86\u4e00\u4e2aISO 8601\u683c\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2aArrow\u5bf9\u8c61\u3002\u5982\u679c\u8f93\u5165\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u683c\u5f0f\u4e0d\u540c\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u683c\u5f0f\u5b57\u7b26\u4e32\u6765\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u89e3\u6790\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32<\/p>\n<p>date_str = &quot;21-11-2023 15:30:00&quot;<\/p>\n<p>date_obj = arrow.get(date_str, &#39;DD-MM-YYYY HH:mm:ss&#39;)<\/p>\n<p>print(date_obj)  # 2023-11-21T15:30:00+00:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>Arrow\u5e93\u53ef\u4ee5\u5c06Arrow\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u683c\u5f0f\u5b57\u7b26\u4e32\u8fdb\u884c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61<\/p>\n<p>formatted_date = date_obj.format(&#39;YYYY-MM-DD HH:mm:ss&#39;)<\/p>\n<p>print(formatted_date)  # 2023-11-21 15:30:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0cArrow\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4f8b\u5982<code>humanize<\/code>\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u4eba\u7c7b\u53ef\u8bfb\u7684\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u683c\u5f0f\u5316\u4e3a\u4eba\u7c7b\u53ef\u8bfb\u7684\u5f62\u5f0f<\/p>\n<p>humanized_date = date_obj.humanize()<\/p>\n<p>print(humanized_date)  # 5 minutes ago<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u65f6\u533a\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>Arrow\u5e93\u4f7f\u5f97\u65f6\u533a\u8f6c\u6362\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>to<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2aArrow\u5bf9\u8c61\u4ece\u4e00\u4e2a\u65f6\u533a\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u65f6\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u65f6\u533a\u8f6c\u6362<\/p>\n<p>date_obj_utc = date_obj.to(&#39;UTC&#39;)<\/p>\n<p>print(date_obj_utc)  # 2023-11-21T15:30:00+00:00<\/p>\n<p>date_obj_local = date_obj.to(&#39;local&#39;)<\/p>\n<p>print(date_obj_local)  # 2023-11-21T10:30:00-05:00 (\u5047\u8bbe\u672c\u5730\u65f6\u533a\u4e3aEST)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u4eceUTC\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u533a\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>replace<\/code>\u65b9\u6cd5\u66f4\u6539\u65f6\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u66f4\u6539\u65f6\u533a<\/p>\n<p>date_obj_new_tz = date_obj.replace(tzinfo=&#39;Europe\/Paris&#39;)<\/p>\n<p>print(date_obj_new_tz)  # 2023-11-21T16:30:00+01:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u52a0\u51cf<\/h3>\n<\/p>\n<p><p>Arrow\u5e93\u8fd8\u652f\u6301\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u52a0\u51cf\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528<code>shift<\/code>\u65b9\u6cd5\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u52a0\u51cf\uff0c\u4f8b\u5982\u589e\u52a0\u6216\u51cf\u5c11\u5929\u6570\u3001\u5c0f\u65f6\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u589e\u52a0\u5929\u6570<\/p>\n<p>new_date = date_obj.shift(days=+3)<\/p>\n<p>print(new_date)  # 2023-11-24T15:30:00+00:00<\/p>\n<h2><strong>\u51cf\u5c11\u5c0f\u65f6\u6570<\/strong><\/h2>\n<p>new_date = date_obj.shift(hours=-5)<\/p>\n<p>print(new_date)  # 2023-11-21T10:30:00+00:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u65f6\u95f4\u8de8\u5ea6<\/h3>\n<\/p>\n<p><p>Arrow\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5904\u7406\u65f6\u95f4\u8de8\u5ea6\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u4e4b\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u65f6\u95f4\u5dee<\/p>\n<p>start_date = arrow.get(&quot;2023-11-21T15:30:00&quot;)<\/p>\n<p>end_date = arrow.get(&quot;2023-12-01T15:30:00&quot;)<\/p>\n<p>time_diff = end_date - start_date<\/p>\n<p>print(time_diff)  # 10 days, 0:00:00<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>range<\/code>\u65b9\u6cd5\u751f\u6210\u4e00\u4e2a\u65e5\u671f\u548c\u65f6\u95f4\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u65e5\u671f\u548c\u65f6\u95f4\u8303\u56f4<\/p>\n<p>start_date = arrow.get(&quot;2023-11-21&quot;)<\/p>\n<p>end_date = arrow.get(&quot;2023-11-25&quot;)<\/p>\n<p>date_range = list(arrow.Arrow.range(&#39;day&#39;, start_date, end_date))<\/p>\n<p>for date in date_range:<\/p>\n<p>    print(date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Arrow\u5e93\u5728Python\u4e2d\u4e3a\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u3002\u901a\u8fc7\u89e3\u6790\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u3001\u8fdb\u884c\u65f6\u533a\u8f6c\u6362\u3001\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u52a0\u51cf\u4ee5\u53ca\u5904\u7406\u65f6\u95f4\u8de8\u5ea6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5e94\u5bf9\u5404\u79cd\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u9700\u6c42\u3002<strong>Arrow\u5e93\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u8fd8\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/strong>\u3002\u65e0\u8bba\u662f\u5904\u7406\u7b80\u5355\u7684\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u8fd8\u662f\u590d\u6742\u7684\u65f6\u533a\u8f6c\u6362\uff0cArrow\u5e93\u90fd\u662f\u4e00\u4e2a\u503c\u5f97\u4fe1\u8d56\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0\u7bad\u5934\u7b26\u53f7\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Unicode\u5b57\u7b26\u6765\u8868\u793a\u7bad\u5934\u7b26\u53f7\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>&quot;\\u2190&quot;<\/code>\u8868\u793a\u5de6\u7bad\u5934\uff0c<code>&quot;\\u2192&quot;<\/code>\u8868\u793a\u53f3\u7bad\u5934\u3002\u901a\u8fc7\u6253\u5370\u8fd9\u4e9b\u5b57\u7b26\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u7bad\u5934\u7684\u663e\u793a\u548c\u8f6c\u6362\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u529f\u80fd\u4e5f\u53ef\u4ee5\u5c06\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u66ff\u6362\u4e3a\u7bad\u5934\u7b26\u53f7\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u7bad\u5934\u7b26\u53f7\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u7bad\u5934\u7b26\u53f7\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u5de6\u7bad\u5934\uff08\u2190\uff09\u3001\u53f3\u7bad\u5934\uff08\u2192\uff09\u3001\u4e0a\u7bad\u5934\uff08\u2191\uff09\u548c\u4e0b\u7bad\u5934\uff08\u2193\uff09\u3002\u8fd9\u4e9b\u7b26\u53f7\u53ef\u4ee5\u901a\u8fc7\u5176Unicode\u7f16\u7801\u6216\u76f4\u63a5\u590d\u5236\u7c98\u8d34\u5230\u4ee3\u7801\u4e2d\u4f7f\u7528\uff0c\u9002\u5408\u5728\u56fe\u5f62\u754c\u9762\u6216\u6587\u672c\u8f93\u51fa\u4e2d\u8868\u793a\u65b9\u5411\u6216\u6307\u793a\u3002<\/p>\n<p><strong>\u5728Python\u7a0b\u5e8f\u4e2d\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u7bad\u5934\u5b57\u7b26\uff1f<\/strong><br \/>\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u7bad\u5934\u5b57\u7b26\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u6bd4\u8f83\u548c\u66ff\u6362\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\u5224\u65ad\u8f93\u5165\u7684\u5b57\u7b26\u662f\u5426\u4e3a\u7bad\u5934\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u662f\u7bad\u5934\uff0c\u53ef\u4ee5\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u80fd\u591f\u5bf9\u4e0d\u540c\u7684\u8f93\u5165\u505a\u51fa\u7075\u6d3b\u53cd\u5e94\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7bad\u5934\uff08Arrow\uff09\u5982\u4f55\u5151\u6362 \u5728Python\u7f16\u7a0b\u4e2d\uff0c\u7bad\u5934\uff08Arrow\uff09\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406 [&hellip;]","protected":false},"author":3,"featured_media":1023285,"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\/1023280"}],"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=1023280"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023280\/revisions"}],"predecessor-version":[{"id":1023288,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1023280\/revisions\/1023288"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1023285"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1023280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1023280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1023280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}