{"id":1073295,"date":"2025-01-08T11:24:09","date_gmt":"2025-01-08T03:24:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073295.html"},"modified":"2025-01-08T11:24:12","modified_gmt":"2025-01-08T03:24:12","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b0%e5%ad%97%e5%8f%98%e6%88%90%e6%97%a5%e6%9c%9f%e6%a0%bc%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073295.html","title":{"rendered":"python\u5982\u4f55\u628a\u6570\u5b57\u53d8\u6210\u65e5\u671f\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103251\/42b31dfd-d4ff-49b5-a14d-df91af4c509d.webp\" alt=\"python\u5982\u4f55\u628a\u6570\u5b57\u53d8\u6210\u65e5\u671f\u683c\u5f0f\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7datetime\u6a21\u5757\u3001pandas\u5e93\u3001strftime\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u6765\u5e2e\u52a9\u7406\u89e3\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/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\u5404\u79cd\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u5b57\u8868\u793a\u7684\u65e5\u671f<\/strong><\/h2>\n<p>date_num = 20231005<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = str(date_num)<\/p>\n<h2><strong>\u4f7f\u7528strptime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date_obj = datetime.strptime(date_str, &#39;%Y%m%d&#39;)<\/p>\n<h2><strong>\u6253\u5370\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002<code>strptime<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&#39;%Y%m%d&#39;<\/code>\u8868\u793a\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u662f\u5e74\u3001\u6708\u3001\u65e5\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>datetime.strptime<\/code> \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u683c\u5f0f\u5b57\u7b26\u4e32\u4e2d\u7684\u5404\u4e2a\u90e8\u5206\u4ee3\u8868\u65e5\u671f\u548c\u65f6\u95f4\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>%Y<\/code> \u8868\u793a\u56db\u4f4d\u6570\u7684\u5e74\u4efd<\/li>\n<li><code>%m<\/code> \u8868\u793a\u4e24\u4f4d\u6570\u7684\u6708\u4efd<\/li>\n<li><code>%d<\/code> \u8868\u793a\u4e24\u4f4d\u6570\u7684\u65e5\u671f<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7ec4\u5408\u8fd9\u4e9b\u683c\u5f0f\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u89e3\u6790\u51e0\u4e4e\u4efb\u4f55\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pandas\u5e93<\/p>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u6377\u7684\u51fd\u6570\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>pd.to_datetime<\/code>\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u5b57\u8868\u793a\u7684\u65e5\u671f<\/strong><\/h2>\n<p>date_num = 20231005<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = str(date_num)<\/p>\n<h2><strong>\u4f7f\u7528pd.to_datetime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date_obj = pd.to_datetime(date_str, format=&#39;%Y%m%d&#39;)<\/p>\n<h2><strong>\u6253\u5370\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>pd.to_datetime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002<code>pd.to_datetime<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>pandas\u5e93\u7684<code>to_datetime<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u5355\u4e2a\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u5305\u542b\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u6574\u4e2a\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u5b57\u8868\u793a\u7684\u65e5\u671f\u5217\u8868<\/strong><\/h2>\n<p>date_nums = [20231005, 20231006, 20231007]<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5217\u8868<\/strong><\/h2>\n<p>date_strs = [str(num) for num in date_nums]<\/p>\n<h2><strong>\u4f7f\u7528pd.to_datetime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date_objs = pd.to_datetime(date_strs, format=&#39;%Y%m%d&#39;)<\/p>\n<h2><strong>\u6253\u5370\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>print(date_objs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u5904\u7406\u5927\u91cf\u65e5\u671f\u6570\u636e\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528strftime\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>strftime<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u5b57\u8868\u793a\u7684\u65e5\u671f<\/strong><\/h2>\n<p>date_num = 20231005<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = str(date_num)<\/p>\n<h2><strong>\u4f7f\u7528strptime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date_obj = datetime.strptime(date_str, &#39;%Y%m%d&#39;)<\/p>\n<h2><strong>\u4f7f\u7528strftime\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>formatted_date = date_obj.strftime(&#39;%Y-%m-%d&#39;)<\/p>\n<h2><strong>\u6253\u5370\u683c\u5f0f\u5316\u7684\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>print(formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<code>strftime<\/code>\u65b9\u6cd5\u7684\u53c2\u6570\u662f\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&#39;%Y-%m-%d&#39;<\/code>\u8868\u793a\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u662f\u5e74-\u6708-\u65e5\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>strftime<\/code>\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u65e5\u671f\u548c\u65f6\u95f4\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>%Y-%m-%d<\/code> \u8868\u793a\u5e74-\u6708-\u65e5\u683c\u5f0f<\/li>\n<li><code>%d\/%m\/%Y<\/code> \u8868\u793a\u65e5\/\u6708\/\u5e74\u683c\u5f0f<\/li>\n<li><code>%A, %B %d, %Y<\/code> \u8868\u793a\u661f\u671f\u51e0, \u6708 \u65e5, \u5e74\u683c\u5f0f<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4e0d\u540c\u7684\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u5f62\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57\u65e5\u671f<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57\u65e5\u671f\uff0c\u4f8b\u5982<code>2023-10-05<\/code>\u3001<code>20231005<\/code>\u3001<code>231005<\/code>\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8c03\u6574\u8f6c\u6362\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5904\u7406\u77ed\u683c\u5f0f\u7684\u6570\u5b57\u65e5\u671f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u77ed\u683c\u5f0f\u7684\u6570\u5b57\u8868\u793a\u7684\u65e5\u671f<\/strong><\/h2>\n<p>date_num = 231005<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = str(date_num)<\/p>\n<h2><strong>\u4f7f\u7528strptime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date_obj = datetime.strptime(date_str, &#39;%y%m%d&#39;)<\/p>\n<h2><strong>\u6253\u5370\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>&#39;%y%m%d&#39;<\/code>\u683c\u5f0f\u89e3\u6790\u77ed\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d<code>%y<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u5e74\u4efd\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ed3\u5408\u4f7f\u7528\u4e0d\u540c\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528pandas\u5e93\u5904\u7406\u5927\u91cf\u65e5\u671f\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528datetime\u6a21\u5757\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u65e5\u671f\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from datetime import datetime<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u5b57\u8868\u793a\u7684\u65e5\u671f\u5217\u8868<\/strong><\/h2>\n<p>date_nums = [20231005, 20231006, 20231007]<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5217\u8868<\/strong><\/h2>\n<p>date_strs = [str(num) for num in date_nums]<\/p>\n<h2><strong>\u4f7f\u7528pd.to_datetime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date_objs = pd.to_datetime(date_strs, format=&#39;%Y%m%d&#39;)<\/p>\n<h2><strong>\u5bf9\u6bcf\u4e2a\u65e5\u671f\u5bf9\u8c61\u4f7f\u7528strftime\u65b9\u6cd5\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>formatted_dates = [date_obj.strftime(&#39;%Y-%m-%d&#39;) for date_obj in date_objs]<\/p>\n<h2><strong>\u6253\u5370\u683c\u5f0f\u5316\u7684\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>print(formatted_dates)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528pandas\u5e93\u5c06\u6570\u5b57\u5217\u8868\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u5c06\u6bcf\u4e2a\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001pandas\u5e93\u3001strftime\u51fd\u6570\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u65e5\u671f\u6570\u636e\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u8be6\u7ec6\u793a\u4f8b\u548c\u89e3\u91ca\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u8868\u793a\u65e5\u671f\u7684\u6574\u6570\uff08\u5982YYYYMMDD\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ndate_string = &quot;20231015&quot;  # \u4f8b\uff1a2023\u5e7410\u670815\u65e5\ndate_object = datetime.strptime(date_string, &quot;%Y%m%d&quot;)\nprint(date_object)  # \u8f93\u51fa\uff1a2023-10-15 00:00:00\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5e93\u8f6c\u6362\u6570\u5b57\u4e3a\u65e5\u671f\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5904\u7406\u65e5\u671f\u7684\u4efb\u52a1\uff0c<code>pandas<\/code>\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u4f7f\u7528<code>pandas.to_datetime()<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndate_number = 20231015  # \u4f8b\uff1a2023\u5e7410\u670815\u65e5\ndate_series = pd.to_datetime(str(date_number), format=&#39;%Y%m%d&#39;)\nprint(date_series)  # \u8f93\u51fa\uff1a2023-10-15 00:00:00\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u91cf\u65e5\u671f\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u65e5\u671f\u4e2d\u7684\u9519\u8bef\u6216\u65e0\u6548\u6570\u5b57\uff1f<\/strong><br \/>\u5728\u8f6c\u6362\u6570\u5b57\u4e3a\u65e5\u671f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6548\u7684\u65e5\u671f\u683c\u5f0f\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ndef convert_to_date(date_string):\n    try:\n        return datetime.strptime(date_string, &quot;%Y%m%d&quot;)\n    except ValueError:\n        return &quot;\u65e0\u6548\u7684\u65e5\u671f\u683c\u5f0f&quot;\n\nprint(convert_to_date(&quot;20231301&quot;))  # \u8f93\u51fa\uff1a\u65e0\u6548\u7684\u65e5\u671f\u683c\u5f0f\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u907f\u514d\u7a0b\u5e8f\u56e0\u65e0\u6548\u8f93\u5165\u800c\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7datetime\u6a21\u5757\u3001pandas\u5e93\u3001strftime\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u672c\u6587\u5c06\u8be6 [&hellip;]","protected":false},"author":3,"featured_media":1073305,"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\/1073295"}],"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=1073295"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073295\/revisions"}],"predecessor-version":[{"id":1073307,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073295\/revisions\/1073307"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073305"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}