{"id":1108425,"date":"2025-01-08T17:00:12","date_gmt":"2025-01-08T09:00:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108425.html"},"modified":"2025-01-08T17:00:15","modified_gmt":"2025-01-08T09:00:15","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%97%a5%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108425.html","title":{"rendered":"Python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072055\/e65c3eac-fdbb-4702-ac2d-2620ad052d43.webp\" alt=\"Python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f\" \/><\/p>\n<p><p> <strong>Python\u5c06\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u6709<code>strptime<\/code>\u51fd\u6570\u3001<code>dateutil<\/code>\u6a21\u5757\u3001<code>pandas<\/code>\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>strptime<\/code>\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5b83\u80fd\u591f\u901a\u8fc7\u6307\u5b9a\u683c\u5f0f\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u6210\u65e5\u671f\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>strptime<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>strptime<\/code>\u51fd\u6570\u5c5e\u4e8e<code>datetime<\/code>\u6a21\u5757\uff0c\u901a\u8fc7<code>datetime.strptime<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002<code>strptime<\/code>\u51fd\u6570\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u662f\u65e5\u671f\u683c\u5f0f\u3002\u65e5\u671f\u683c\u5f0f\u6307\u660e\u4e86\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u6392\u5217\u987a\u5e8f\u548c\u5f62\u5f0f\uff0c\u4f8b\u5982<code>&quot;%Y-%m-%d&quot;<\/code>\u8868\u793a&quot;\u5e74-\u6708-\u65e5&quot;\u7684\u5f62\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>strptime<\/code>\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>date_string = &quot;2023-10-15&quot;<\/p>\n<p>date_format = &quot;%Y-%m-%d&quot;<\/p>\n<p>date_object = datetime.strptime(date_string, date_format)<\/p>\n<p>print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;2023-10-15&quot;<\/code>\u6309\u7167<code>&quot;%Y-%m-%d&quot;<\/code>\u7684\u683c\u5f0f\u88ab\u89e3\u6790\u4e3a\u65e5\u671f\u5bf9\u8c61<code>datetime(2023, 10, 15, 0, 0)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001strptime\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>strptime<\/code>\u51fd\u6570\u662fPython\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u7684\u57fa\u672c\u65b9\u6cd5\u3002\u5b83\u6765\u81ea<code>datetime<\/code>\u6a21\u5757\uff0c\u901a\u8fc7<code>datetime.strptime<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7cbe\u51c6\u5730\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u6210\u65e5\u671f\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>strptime<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u63d0\u4f9b\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u662f\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u53e6\u4e00\u4e2a\u662f\u65e5\u671f\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>date_string = &quot;2023-10-15&quot;<\/p>\n<p>date_format = &quot;%Y-%m-%d&quot;<\/p>\n<p>date_object = datetime.strptime(date_string, date_format)<\/p>\n<p>print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>date_string<\/code>\u662f\u5f85\u8f6c\u6362\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c<code>date_format<\/code>\u662f\u8be5\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65e5\u671f\u5bf9\u8c61<code>datetime(2023, 10, 15, 0, 0)<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e38\u89c1\u65e5\u671f\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>strptime<\/code>\u51fd\u6570\u65f6\uff0c\u7406\u89e3\u65e5\u671f\u683c\u5f0f\u7684\u5199\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65e5\u671f\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&quot;%Y-%m-%d&quot;<\/code>\uff1a\u5e74-\u6708-\u65e5<\/li>\n<li><code>&quot;%d\/%m\/%Y&quot;<\/code>\uff1a\u65e5\/\u6708\/\u5e74<\/li>\n<li><code>&quot;%m-%d-%Y %H:%M:%S&quot;<\/code>\uff1a\u6708-\u65e5-\u5e74 \u5c0f\u65f6:\u5206\u949f:\u79d2<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_string = &quot;15\/10\/2023&quot;<\/p>\n<p>date_format = &quot;%d\/%m\/%Y&quot;<\/p>\n<p>date_object = datetime.strptime(date_string, date_format)<\/p>\n<p>print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;15\/10\/2023&quot;<\/code>\u6309\u7167<code>&quot;%d\/%m\/%Y&quot;<\/code>\u7684\u683c\u5f0f\u88ab\u89e3\u6790\u4e3a\u65e5\u671f\u5bf9\u8c61<code>datetime(2023, 10, 15, 0, 0)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001dateutil\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>dateutil<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u548c\u89e3\u6790\u5404\u79cd\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u65e5\u671f\u5904\u7406\u7684\u590d\u6742\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5<code>dateutil<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install python-dateutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5bfc\u5165<code>parser<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil import parser<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>dateutil.parser.parse<\/code>\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u89e3\u6790\u5927\u591a\u6570\u5e38\u89c1\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u65e0\u9700\u663e\u5f0f\u6307\u5b9a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil import parser<\/p>\n<p>date_string = &quot;2023-10-15 14:30:00&quot;<\/p>\n<p>date_object = parser.parse(date_string)<\/p>\n<p>print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>parser.parse<\/code>\u81ea\u52a8\u8bc6\u522b\u5e76\u89e3\u6790\u4e86\u65e5\u671f\u5b57\u7b26\u4e32<code>&quot;2023-10-15 14:30:00&quot;<\/code>\uff0c\u751f\u6210\u65e5\u671f\u5bf9\u8c61<code>datetime(2023, 10, 15, 14, 30)<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u4e0d\u540c\u683c\u5f0f<\/h4>\n<\/p>\n<p><p><code>dateutil<\/code>\u6a21\u5757\u7279\u522b\u64c5\u957f\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u4e0d\u89c4\u5219\u7684\u6216\u8005\u6df7\u5408\u4f7f\u7528\u4e0d\u540c\u5206\u9694\u7b26\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_string = &quot;15th Oct, 2023 02:30 PM&quot;<\/p>\n<p>date_object = parser.parse(date_string)<\/p>\n<p>print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>parser.parse<\/code>\u80fd\u591f\u8bc6\u522b\u5e76\u89e3\u6790\u5305\u542b\u6587\u672c\u7684\u65e5\u671f\u5b57\u7b26\u4e32<code>&quot;15th Oct, 2023 02:30 PM&quot;<\/code>\uff0c\u751f\u6210\u65e5\u671f\u5bf9\u8c61<code>datetime(2023, 10, 15, 14, 30)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001pandas\u5e93<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u65e5\u671f\u5904\u7406\u529f\u80fd\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf\u65e5\u671f\u6570\u636e\u3002<code>pandas.to_datetime<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5bfc\u5165<code>pandas<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>pandas.to_datetime<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u5e76\u4e14\u53ef\u4ee5\u5904\u7406\u7f3a\u5931\u503c\u548c\u5f02\u5e38\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>date_string = &quot;2023-10-15&quot;<\/p>\n<p>date_object = pd.to_datetime(date_string)<\/p>\n<p>print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>pd.to_datetime<\/code>\u51fd\u6570\u5c06\u65e5\u671f\u5b57\u7b26\u4e32<code>&quot;2023-10-15&quot;<\/code>\u89e3\u6790\u4e3a<code>Timestamp(&#39;2023-10-15 00:00:00&#39;)<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u591a\u4e2a\u65e5\u671f\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u7279\u522b\u9002\u5408\u5904\u7406\u5305\u542b\u591a\u4e2a\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u5e8f\u5217\u6216\u8005DataFrame\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6574\u4e2a\u5e8f\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_strings = [&quot;2023-10-15&quot;, &quot;2023\/10\/16&quot;, &quot;15-10-2023&quot;]<\/p>\n<p>date_objects = pd.to_datetime(date_strings)<\/p>\n<p>print(date_objects)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>pd.to_datetime<\/code>\u51fd\u6570\u80fd\u591f\u8bc6\u522b\u5e76\u89e3\u6790\u591a\u79cd\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u751f\u6210\u65e5\u671f\u5bf9\u8c61\u5e8f\u5217<code>DatetimeIndex([&#39;2023-10-15&#39;, &#39;2023-10-16&#39;, &#39;2023-10-15&#39;], dtype=&#39;datetime64[ns]&#39;, freq=None)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u81ea\u5b9a\u4e49\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u81ea\u5b9a\u4e49\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from datetime import datetime<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9a\u4e49\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5339\u914d\u7684\u90e8\u5206\u63d0\u53d6\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_string = &quot;15-Oct-2023 14:30:00&quot;<\/p>\n<p>regex = r&quot;(\\d{2})-(\\w{3})-(\\d{4}) (\\d{2}):(\\d{2}):(\\d{2})&quot;<\/p>\n<p>match = re.match(regex, date_string)<\/p>\n<p>if match:<\/p>\n<p>    day, month_abbr, year, hour, minute, second = match.groups()<\/p>\n<p>    month = datetime.strptime(month_abbr, &quot;%b&quot;).month<\/p>\n<p>    date_object = datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))<\/p>\n<p>    print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>r&quot;(\\d{2})-(\\w{3})-(\\d{4}) (\\d{2}):(\\d{2}):(\\d{2})&quot;<\/code>\u5339\u914d\u65e5\u671f\u5b57\u7b26\u4e32<code>&quot;15-Oct-2023 14:30:00&quot;<\/code>\u5e76\u63d0\u53d6\u51fa\u5404\u4e2a\u90e8\u5206\uff0c\u6700\u7ec8\u89e3\u6790\u4e3a\u65e5\u671f\u5bf9\u8c61<code>datetime(2023, 10, 15, 14, 30, 0)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u4e0d\u540c\u7684\u65f6\u533a<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u5e26\u6709\u65f6\u533a\u4fe1\u606f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002Python\u7684<code>pytz<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u65f6\u533a\u76f8\u5173\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5<code>pytz<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pytz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u89e3\u6790\u65f6\u533a\u65e5\u671f\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p><code>dateutil<\/code>\u6a21\u5757\u7684<code>parser.parse<\/code>\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u65f6\u533a\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u89e3\u6790\u4e3a\u5e26\u65f6\u533a\u7684\u65e5\u671f\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil import parser<\/p>\n<p>date_string = &quot;2023-10-15T14:30:00-04:00&quot;<\/p>\n<p>date_object = parser.parse(date_string)<\/p>\n<p>print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>parser.parse<\/code>\u51fd\u6570\u81ea\u52a8\u8bc6\u522b\u5e76\u89e3\u6790\u4e86\u5e26\u6709\u65f6\u533a\u4fe1\u606f\u7684\u65e5\u671f\u5b57\u7b26\u4e32<code>&quot;2023-10-15T14:30:00-04:00&quot;<\/code>\uff0c\u751f\u6210\u65e5\u671f\u5bf9\u8c61<code>datetime(2023, 10, 15, 14, 30, tzinfo=tzoffset(None, -14400))<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u624b\u52a8\u6dfb\u52a0\u65f6\u533a\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u624b\u52a8\u4e3a\u65e5\u671f\u5bf9\u8c61\u6dfb\u52a0\u65f6\u533a\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u6a21\u5757\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pytz import timezone<\/p>\n<p>date_string = &quot;2023-10-15 14:30:00&quot;<\/p>\n<p>date_format = &quot;%Y-%m-%d %H:%M:%S&quot;<\/p>\n<p>date_object = datetime.strptime(date_string, date_format)<\/p>\n<p>eastern = timezone(&#39;US\/Eastern&#39;)<\/p>\n<p>date_object = eastern.localize(date_object)<\/p>\n<p>print(date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u624b\u52a8\u4e3a\u65e5\u671f\u5bf9\u8c61\u6dfb\u52a0\u4e86\u7f8e\u56fd\u4e1c\u90e8\u65f6\u533a\u4fe1\u606f\uff0c\u6700\u7ec8\u751f\u6210\u7684\u65e5\u671f\u5bf9\u8c61\u4e3a<code>datetime(2023, 10, 15, 14, 30, tzinfo=&lt;DstTzInfo &#39;US\/Eastern&#39; EDT-1 day, 20:00:00 DST&gt;)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec<code>strptime<\/code>\u51fd\u6570\u3001<code>dateutil<\/code>\u6a21\u5757\u3001<code>pandas<\/code>\u5e93\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u81ea\u5b9a\u4e49\u89e3\u6790\u7b49\u3002<strong>\u5176\u4e2d\uff0c<code>strptime<\/code>\u51fd\u6570\u662f\u6700\u57fa\u672c\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c<code>dateutil<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u81ea\u52a8\u89e3\u6790\u529f\u80fd\uff0c<code>pandas<\/code>\u5e93\u9002\u5408\u5904\u7406\u5927\u91cf\u65e5\u671f\u6570\u636e\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u81ea\u5b9a\u4e49\u89e3\u6790\u9002\u7528\u4e8e\u7279\u6b8a\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c<code>pytz<\/code>\u6a21\u5757\u5219\u5e2e\u52a9\u5904\u7406\u65f6\u533a\u4fe1\u606f<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u65e5\u671f\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u4f60\u5b9a\u4e49\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\uff0c\u4ee5\u4fbf\u6b63\u786e\u89e3\u6790\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u5c06\u5b57\u7b26\u4e32\u201c2023-10-05\u201d\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ndate_string = &quot;2023-10-05&quot;\ndate_object = datetime.strptime(date_string, &quot;%Y-%m-%d&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c<code>%Y<\/code>\u4ee3\u8868\u56db\u4f4d\u6570\u7684\u5e74\u4efd\uff0c<code>%m<\/code>\u4ee3\u8868\u6708\u4efd\uff0c<code>%d<\/code>\u4ee3\u8868\u65e5\u671f\u3002\u6839\u636e\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8c03\u6574\u8fd9\u4e9b\u683c\u5f0f\u7b26\u53f7\u3002<\/p>\n<p><strong>\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u5904\u7406\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\u65f6\uff0c\u60a8\u9700\u8981\u6839\u636e\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u5b9e\u9645\u683c\u5f0f\u8c03\u6574<code>strptime<\/code>\u4e2d\u7684\u683c\u5f0f\u53c2\u6570\u3002\u6bd4\u5982\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u662f\u201c05\/10\/2023\u201d\uff0c\u5bf9\u5e94\u7684\u683c\u5f0f\u5e94\u8be5\u662f<code>&quot;%d\/%m\/%Y&quot;<\/code>\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">date_string = &quot;05\/10\/2023&quot;\ndate_object = datetime.strptime(date_string, &quot;%d\/%m\/%Y&quot;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u683c\u5f0f\u7b26\u53f7\u4e0e\u5b57\u7b26\u4e32\u683c\u5f0f\u4e00\u81f4\uff0c\u4ee5\u907f\u514d\u89e3\u6790\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u7684\u65e5\u671f\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u5b57\u7b26\u4e32\u65e5\u671f\u683c\u5f0f\u4e0d\u6b63\u786e\u65f6\uff0c\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u4e3a\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5e76\u5904\u7406\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u76f8\u5173\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">date_string = &quot;2023-31-05&quot;  # \u9519\u8bef\u7684\u65e5\u671f\u683c\u5f0f\ntry:\n    date_object = datetime.strptime(date_string, &quot;%Y-%m-%d&quot;)\nexcept ValueError:\n    print(&quot;\u65e5\u671f\u683c\u5f0f\u9519\u8bef\uff0c\u8bf7\u68c0\u67e5\u8f93\u5165\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6355\u83b7\u9519\u8bef\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u6709strptime\u51fd\u6570\u3001dateutil\u6a21\u5757\u3001pandas\u5e93\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1108434,"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\/1108425"}],"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=1108425"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108425\/revisions"}],"predecessor-version":[{"id":1108435,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108425\/revisions\/1108435"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108434"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}