{"id":1141026,"date":"2025-01-08T22:27:17","date_gmt":"2025-01-08T14:27:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141026.html"},"modified":"2025-01-08T22:27:20","modified_gmt":"2025-01-08T14:27:20","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e5%ad%97%e8%bd%ac%e5%8f%98%e6%88%90%e6%97%a5%e6%9c%9f%e6%a0%bc%e5%bc%8fpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141026.html","title":{"rendered":"\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u53d8\u6210\u65e5\u671f\u683c\u5f0fpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103845\/374e85a4-eec7-40f7-b4af-23ebd756e7a9.webp\" alt=\"\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u53d8\u6210\u65e5\u671f\u683c\u5f0fpython\" \/><\/p>\n<p><p> <strong>\u5c06\u6570\u5b57\u8f6c\u53d8\u6210\u65e5\u671f\u683c\u5f0f\u7684\u51e0\u79cd\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528pandas\u5e93\u3001\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u8f6c\u6362<\/strong>\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528datetime\u6a21\u5757\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528datetime\u6a21\u5757\u8fdb\u884c\u6570\u5b57\u8f6c\u65e5\u671f\u7684\u8f6c\u6362\u975e\u5e38\u9ad8\u6548\u548c\u7075\u6d3b\u3002\u9996\u5148\uff0c\u9700\u8981\u5c06\u6570\u5b57\u89e3\u6790\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528datetime\u6a21\u5757\u7684strptime\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528datetime\u6a21\u5757<\/strong><\/h2>\n<p><p>\u4f7f\u7528datetime\u6a21\u5757\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u6570\u5b57\u89e3\u6790\u4e3a\u5b57\u7b26\u4e32\u5e76\u4f7f\u7528datetime.strptime\u65b9\u6cd5\u6765\u5b9e\u73b0\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u5b57\u662f\u201c20231005\u201d\u5f62\u5f0f\uff0c\u8868\u793a\u201c2023-10-05\u201d<\/strong><\/h2>\n<p>date_str = str(20231005)<\/p>\n<p>date_obj = datetime.strptime(date_str, &#39;%Y%m%d&#39;)<\/p>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528datetime.strptime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002datetime.strptime\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u548c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002\u8fd9\u91cc\u683c\u5f0f\u662f\u201c%Y%m%d\u201d\uff0c\u8868\u793a\u5e74\u4efd\u3001\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528pandas\u5e93<\/h2>\n<\/p>\n<p><p>Pandas\u5e93\u5728\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\u975e\u5e38\u5f3a\u5927\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pandas.to_datetime\u65b9\u6cd5\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\u6570\u5b57\u662f\u201c20231005\u201d\u5f62\u5f0f\uff0c\u8868\u793a\u201c2023-10-05\u201d<\/strong><\/h2>\n<p>date_num = 20231005<\/p>\n<p>date_str = str(date_num)<\/p>\n<p>date_obj = pd.to_datetime(date_str, format=&#39;%Y%m%d&#39;)<\/p>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas.to_datetime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u7c7b\u4f3c\u4e8edatetime.strptime\u65b9\u6cd5\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u4e32\u548c\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u624b\u52a8\u89e3\u6790\u6570\u5b57\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u8868\u793a\u5e74\u3001\u6708\u548c\u65e5\u7684\u6570\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u5c06\u5176\u89e3\u6790\u4e3a\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6570\u5b57\u662f\u201c20231005\u201d\u5f62\u5f0f\uff0c\u8868\u793a\u201c2023-10-05\u201d<\/p>\n<p>date_num = 20231005<\/p>\n<p>date_str = str(date_num)<\/p>\n<p>year = date_str[:4]<\/p>\n<p>month = date_str[4:6]<\/p>\n<p>day = date_str[6:]<\/p>\n<p>date_obj = f&#39;{year}-{month}-{day}&#39;<\/p>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u5b57\u89e3\u6790\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u63d0\u53d6\u5e74\u4efd\u3001\u6708\u4efd\u548c\u65e5\u671f\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5c06\u5176\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u5b57\u53ef\u80fd\u4ee5\u4e0d\u540c\u7684\u683c\u5f0f\u8868\u793a\u65e5\u671f\u3002\u4f8b\u5982\uff0c\u6570\u5b57\u53ef\u80fd\u4ee5\u201cYYMMDD\u201d\u6216\u201cYYYYDDMM\u201d\u5f62\u5f0f\u8868\u793a\u65e5\u671f\u3002\u6211\u4eec\u9700\u8981\u6839\u636e\u6570\u5b57\u7684\u683c\u5f0f\u8c03\u6574\u89e3\u6790\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5904\u7406\u201cYYMMDD\u201d\u683c\u5f0f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u5b57\u662f\u201c231005\u201d\u5f62\u5f0f\uff0c\u8868\u793a\u201c2023-10-05\u201d<\/strong><\/h2>\n<p>date_str = str(231005)<\/p>\n<p>date_obj = datetime.strptime(date_str, &#39;%y%m%d&#39;)<\/p>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u201c%y%m%d\u201d\u683c\u5f0f\u5c06\u6570\u5b57\u89e3\u6790\u4e3a\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u201cYYYYDDMM\u201d\u683c\u5f0f<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u5b57\u662f\u201c20230510\u201d\u5f62\u5f0f\uff0c\u8868\u793a\u201c2023-10-05\u201d<\/strong><\/h2>\n<p>date_str = str(20230510)<\/p>\n<p>date_obj = datetime.strptime(date_str, &#39;%Y%d%m&#39;)<\/p>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u201c%Y%d%m\u201d\u683c\u5f0f\u5c06\u6570\u5b57\u89e3\u6790\u4e3a\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5904\u7406\u65e0\u6548\u65e5\u671f<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6548\u65e5\u671f\u3002\u4f8b\u5982\uff0c\u6570\u5b57\u53ef\u80fd\u8868\u793a\u4e0d\u5b58\u5728\u7684\u65e5\u671f\uff08\u598220230230\uff09\u3002\u6211\u4eec\u9700\u8981\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>def convert_to_date(date_num):<\/p>\n<p>    try:<\/p>\n<p>        date_str = str(date_num)<\/p>\n<p>        date_obj = datetime.strptime(date_str, &#39;%Y%m%d&#39;)<\/p>\n<p>        return date_obj<\/p>\n<p>    except ValueError:<\/p>\n<p>        return None<\/p>\n<p>date_num = 20230230<\/p>\n<p>date_obj = convert_to_date(date_num)<\/p>\n<p>if date_obj:<\/p>\n<p>    print(date_obj)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Invalid date&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528try-except\u5757\u6355\u83b7\u65e0\u6548\u65e5\u671f\u9519\u8bef\u3002\u5982\u679c\u65e5\u671f\u65e0\u6548\uff0c\u6211\u4eec\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5904\u7406\u65f6\u95f4\u6233<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u65f6\u95f4\u6233\u8868\u793a\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u79d2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u7684fromtimestamp\u65b9\u6cd5\u5c06\u5176\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\u65f6\u95f4\u6233\u662f\u201c1696416000\u201d\uff0c\u8868\u793a\u201c2023-10-05\u201d<\/strong><\/h2>\n<p>timestamp = 1696416000<\/p>\n<p>date_obj = datetime.fromtimestamp(timestamp)<\/p>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528datetime.fromtimestamp\u65b9\u6cd5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8868\u793a\u968f\u65f6\u95f4\u53d8\u5316\u7684\u6570\u636e\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u662f\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\uff0c\u8868\u793a\u65e5\u671f<\/strong><\/h2>\n<p>date_nums = [20231001, 20231002, 20231003, 20231004, 20231005]<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f<\/strong><\/h2>\n<p>dates = pd.to_datetime([str(date_num) for date_num in date_nums], format=&#39;%Y%m%d&#39;)<\/p>\n<p>print(dates)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u5217\u8868\u3002\u6211\u4eec\u4f7f\u7528pandas.to_datetime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u53ef\u80fd\u4ee5\u4e0d\u540c\u7684\u683c\u5f0f\u8868\u793a\u65e5\u671f\u3002\u4f8b\u5982\uff0c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u53ef\u80fd\u4ee5\u201cYYMMDD\u201d\u6216\u201cYYYYDDMM\u201d\u5f62\u5f0f\u8868\u793a\u65e5\u671f\u3002\u6211\u4eec\u9700\u8981\u6839\u636e\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u683c\u5f0f\u8c03\u6574\u89e3\u6790\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5904\u7406\u201cYYMMDD\u201d\u683c\u5f0f\u7684\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u662f\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\uff0c\u8868\u793a\u65e5\u671f<\/strong><\/h2>\n<p>date_nums = [231001, 231002, 231003, 231004, 231005]<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f<\/strong><\/h2>\n<p>dates = pd.to_datetime([str(date_num) for date_num in date_nums], format=&#39;%y%m%d&#39;)<\/p>\n<p>print(dates)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u201c%y%m%d\u201d\u683c\u5f0f\u5c06\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u201cYYYYDDMM\u201d\u683c\u5f0f\u7684\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u662f\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\uff0c\u8868\u793a\u65e5\u671f<\/strong><\/h2>\n<p>date_nums = [20230501, 20230502, 20230503, 20230504, 20230505]<\/p>\n<h2><strong>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f<\/strong><\/h2>\n<p>dates = pd.to_datetime([str(date_num) for date_num in date_nums], format=&#39;%Y%d%m&#39;)<\/p>\n<p>print(dates)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u201c%Y%d%m\u201d\u683c\u5f0f\u5c06\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u5904\u7406\u5177\u6709\u65f6\u533a\u7684\u65e5\u671f<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u5177\u6709\u65f6\u533a\u7684\u65e5\u671f\u3002\u65f6\u533a\u8868\u793a\u4e0d\u540c\u5730\u533a\u7684\u65f6\u95f4\u5dee\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u5904\u7406\u5177\u6709\u65f6\u533a\u7684\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u5b57\u662f\u201c20231005\u201d\u5f62\u5f0f\uff0c\u8868\u793a\u201c2023-10-05\u201d<\/strong><\/h2>\n<p>date_num = 20231005<\/p>\n<p>date_str = str(date_num)<\/p>\n<p>date_obj = pd.to_datetime(date_str, format=&#39;%Y%m%d&#39;)<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u5177\u6709\u65f6\u533a\u7684\u65e5\u671f<\/strong><\/h2>\n<p>date_obj = date_obj.tz_localize(&#39;UTC&#39;)<\/p>\n<p>print(date_obj)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas.to_datetime\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528tz_localize\u65b9\u6cd5\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u5177\u6709\u65f6\u533a\u7684\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001pandas\u5e93\u548c\u5b57\u7b26\u4e32\u683c\u5f0f\u8f6c\u6362\u6765\u5b9e\u73b0\u8f6c\u6362\u3002\u5728\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57\u3001\u65e0\u6548\u65e5\u671f\u3001\u65f6\u95f4\u6233\u548c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u5728\u5904\u7406\u5177\u6709\u65f6\u533a\u7684\u65e5\u671f\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u4e2d\u7684tz_localize\u65b9\u6cd5\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u8fc7\u7a0b\u4e2d\u9ad8\u6548\u5730\u5904\u7406\u65e5\u671f\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\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\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6570\u5b57\u8868\u793a\u65e5\u671f\u7684\u65b9\u5f0f\u53ef\u80fd\u662f\u65f6\u95f4\u6233\u6216\u7279\u5b9a\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002\u901a\u8fc7<code>datetime.fromtimestamp()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65e5\u671f\uff0c\u6216\u8005\u4f7f\u7528<code>strptime()<\/code>\u65b9\u6cd5\u5c06\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u8f6c\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u5b9e\u73b0\u6570\u5b57\u5230\u65e5\u671f\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684<code>datetime<\/code>\u6a21\u5757\uff0c<code>pandas<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u671f\u5904\u7406\u529f\u80fd\u3002\u4f7f\u7528<code>pd.to_datetime()<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5c06\u591a\u79cd\u683c\u5f0f\u7684\u6570\u5b57\uff08\u5982\u65f6\u95f4\u6233\u6216\u5b57\u7b26\u4e32\uff09\u8f6c\u6362\u4e3a\u65e5\u671f\u3002\u8fd9\u4f7f\u5f97\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u66f4\u52a0\u4fbf\u6377\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u65b9\u9762\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57\u4ee5\u786e\u4fdd\u6b63\u786e\u7684\u65e5\u671f\u8f6c\u6362\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u5b57\u8f6c\u6362\u65f6\uff0c\u786e\u4fdd\u4e86\u89e3\u6570\u5b57\u7684\u5177\u4f53\u683c\u5f0f\u975e\u5e38\u91cd\u8981\u3002\u5982\u679c\u662f\u65f6\u95f4\u6233\uff0c\u8bf7\u786e\u8ba4\u5b83\u662f\u4ee5\u79d2\u6216\u6beb\u79d2\u4e3a\u5355\u4f4d\u3002\u5982\u679c\u662f\u4ee5\u67d0\u79cd\u7279\u5b9a\u683c\u5f0f\u8868\u793a\u7684\u65e5\u671f\u6570\u5b57\uff08\u4f8b\u5982\uff1aYYYYMMDD\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strptime()<\/code>\u65b9\u6cd5\u5e76\u6307\u5b9a\u76f8\u5e94\u7684\u683c\u5f0f\u5b57\u7b26\u4e32\u6765\u51c6\u786e\u89e3\u6790\u3002\u5c06\u683c\u5f0f\u5904\u7406\u5f97\u5f53\uff0c\u53ef\u4ee5\u907f\u514d\u8f6c\u6362\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06\u6570\u5b57\u8f6c\u53d8\u6210\u65e5\u671f\u683c\u5f0f\u7684\u51e0\u79cd\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528pandas\u5e93\u3001\u8fdb\u884c\u5b57\u7b26\u4e32\u683c\u5f0f\u8f6c\u6362\u3002\u63a5\u4e0b\u6765\u6211\u4eec [&hellip;]","protected":false},"author":3,"featured_media":1141034,"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\/1141026"}],"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=1141026"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141026\/revisions"}],"predecessor-version":[{"id":1141036,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141026\/revisions\/1141036"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141034"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}