{"id":1086669,"date":"2025-01-08T13:27:53","date_gmt":"2025-01-08T05:27:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1086669.html"},"modified":"2025-01-08T13:27:55","modified_gmt":"2025-01-08T05:27:55","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e7%bb%99%e5%ae%9a%e6%97%a5%e6%9c%9f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1086669.html","title":{"rendered":"Python\u5982\u4f55\u8f93\u51fa\u7ed9\u5b9a\u65e5\u671f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195950\/154fb605-f01b-46bf-a953-f4f7c37bbe4f.webp\" alt=\"Python\u5982\u4f55\u8f93\u51fa\u7ed9\u5b9a\u65e5\u671f\" \/><\/p>\n<p><p> <strong>Python\u8f93\u51fa\u7ed9\u5b9a\u65e5\u671f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u6a21\u5757\u7b49\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528pandas\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u6700\u7075\u6d3b\u7684\u662fdatetime\u6a21\u5757\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecddatetime\u6a21\u5757\u7684\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528datetime\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u7c7b\u548c\u65b9\u6cd5\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u8981\u8f93\u51fa\u7ed9\u5b9a\u65e5\u671f\uff0c\u53ef\u4ee5\u4f7f\u7528datetime.date\u7c7b\u3002datetime\u6a21\u5757\u53ef\u4ee5\u8fdb\u884c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u8fd0\u7b97\u3001\u683c\u5f0f\u5316\u7b49\u64cd\u4f5c\uff0c\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5bfc\u5165datetime\u6a21\u5757\u5e76\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165datetime\u6a21\u5757\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528datetime.date\u7c7b\u6765\u521b\u5efa\u65e5\u671f\u5bf9\u8c61\uff0c\u683c\u5f0f\u4e3adatetime.date(year, month, day)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>given_date = datetime.date(2023, 10, 15)<\/p>\n<p>print(given_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528strftime\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0cstrftime\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u683c\u5f0f\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e38\u89c1\u7684\u683c\u5f0f\u5b57\u7b26\u5305\u62ec\uff1a%Y\uff08\u56db\u4f4d\u6570\u5e74\u4efd\uff09\u3001%m\uff08\u4e24\u4f4d\u6570\u6708\u4efd\uff09\u3001%d\uff08\u4e24\u4f4d\u6570\u65e5\u671f\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>given_date = datetime.date(2023, 10, 15)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f<\/strong><\/h2>\n<p>formatted_date = given_date.strftime(&quot;%Y-%m-%d&quot;)<\/p>\n<p>print(formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528strptime\u65b9\u6cd5\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0cstrptime\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u65e5\u671f\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u683c\u5f0f\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &quot;2023-10-15&quot;<\/p>\n<h2><strong>\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>parsed_date = datetime.datetime.strptime(date_str, &quot;%Y-%m-%d&quot;).date()<\/p>\n<p>print(parsed_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u83b7\u53d6\u5f53\u524d\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528datetime.date.today\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f<\/strong><\/h2>\n<p>current_date = datetime.date.today()<\/p>\n<p>print(current_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528time\u6a21\u5757<\/p>\n<\/p>\n<p><p>time\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65f6\u95f4\u76f8\u5173\u7684\u51fd\u6570\uff0c\u4f46\u5176\u529f\u80fd\u4e0d\u5982datetime\u6a21\u5757\u5f3a\u5927\u3002\u53ef\u4ee5\u4f7f\u7528time.strftime\u548ctime.strptime\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u548c\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>given_date = time.strptime(&quot;2023-10-15&quot;, &quot;%Y-%m-%d&quot;)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f<\/strong><\/h2>\n<p>formatted_date = time.strftime(&quot;%Y-%m-%d&quot;, given_date)<\/p>\n<p>print(formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &quot;2023-10-15&quot;<\/p>\n<h2><strong>\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>parsed_date = time.strptime(date_str, &quot;%Y-%m-%d&quot;)<\/p>\n<p>print(parsed_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pandas\u6a21\u5757<\/p>\n<\/p>\n<p><p>pandas\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528pandas.to_datetime\u65b9\u6cd5\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528strftime\u65b9\u6cd5\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5bfc\u5165pandas\u6a21\u5757\u5e76\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &quot;2023-10-15&quot;<\/p>\n<h2><strong>\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>parsed_date = pd.to_datetime(date_str)<\/p>\n<p>print(parsed_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &quot;2023-10-15&quot;<\/p>\n<h2><strong>\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>parsed_date = pd.to_datetime(date_str)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f<\/strong><\/h2>\n<p>formatted_date = parsed_date.strftime(&quot;%Y-%m-%d&quot;)<\/p>\n<p>print(formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u65e5\u671f\u8fd0\u7b97<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u8fdb\u884c\u65e5\u671f\u7684\u52a0\u51cf\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528datetime.timedelta\u7c7b\u6765\u8868\u793a\u65f6\u95f4\u95f4\u9694\uff0c\u5e76\u8fdb\u884c\u65e5\u671f\u7684\u52a0\u51cf\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u65e5\u671f\u52a0\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>given_date = datetime.date(2023, 10, 15)<\/p>\n<h2><strong>\u65e5\u671f\u52a0\u6cd5<\/strong><\/h2>\n<p>new_date = given_date + datetime.timedelta(days=10)<\/p>\n<p>print(new_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u65e5\u671f\u51cf\u6cd5<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>given_date = datetime.date(2023, 10, 15)<\/p>\n<h2><strong>\u65e5\u671f\u51cf\u6cd5<\/strong><\/h2>\n<p>new_date = given_date - datetime.timedelta(days=10)<\/p>\n<p>print(new_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u6700\u5e38\u7528\u548c\u6700\u7075\u6d3b\u7684\u662fdatetime\u6a21\u5757\u3002\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u521b\u5efa\u65e5\u671f\u5bf9\u8c61\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u65e5\u671f\u3001\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32\u3001\u83b7\u53d6\u5f53\u524d\u65e5\u671f\uff0c\u5e76\u8fdb\u884c\u65e5\u671f\u8fd0\u7b97\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528time\u6a21\u5757\u548cpandas\u6a21\u5757\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u76f8\u5173\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>datetime.datetime.now()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u7cfb\u7edf\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\ncurrent_datetime = datetime.datetime.now()\nprint(current_datetime)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\u7279\u5b9a\u7684\u65e5\u671f\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u65e5\u671f\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u5c06\u65e5\u671f\u683c\u5f0f\u5316\u4e3a\u201cYYYY-MM-DD\u201d\u7684\u5f62\u5f0f\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\nspecific_date = datetime.datetime(2023, 10, 5)\nformatted_date = specific_date.strftime(&quot;%Y-%m-%d&quot;)\nprint(formatted_date)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u201c2023-10-05\u201d\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u6539\u683c\u5f0f\u5b57\u7b26\u4e32\u4ee5\u5339\u914d\u60a8\u7684\u8981\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\uff1f<\/strong><br \/>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u53ea\u9700\u5c06\u4e24\u4e2a\u65e5\u671f\u76f8\u51cf\uff0c\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\uff0c\u8868\u793a\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\ndate1 = datetime.datetime(2023, 10, 1)\ndate2 = datetime.datetime(2023, 10, 5)\ndifference = date2 - date1\nprint(difference.days)  # \u8f93\u51fa\u5dee\u5f02\u7684\u5929\u6570\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa4\uff0c\u8868\u793a\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\u4e3a4\u5929\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8f93\u51fa\u7ed9\u5b9a\u65e5\u671f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u6a21\u5757\u7b49\u3002\u5e38\u89c1\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1086674,"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\/1086669"}],"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=1086669"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086669\/revisions"}],"predecessor-version":[{"id":1086677,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086669\/revisions\/1086677"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1086674"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1086669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1086669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1086669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}