{"id":994110,"date":"2024-12-27T08:52:26","date_gmt":"2024-12-27T00:52:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994110.html"},"modified":"2024-12-27T08:52:32","modified_gmt":"2024-12-27T00:52:32","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e6%97%a5%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994110.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8f93\u5165\u65e5\u671f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071235\/ed4b4c1f-9bc4-4b5e-ae79-3ed9f9f433a9.webp\" alt=\"python\u4e2d\u5982\u4f55\u8f93\u5165\u65e5\u671f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8f93\u5165\u65e5\u671f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u8ba9\u7528\u6237\u624b\u52a8\u8f93\u5165\u3001\u4f7f\u7528datetime\u6a21\u5757\u89e3\u6790\u5b57\u7b26\u4e32\u3001\u901a\u8fc7\u7b2c\u4e09\u65b9\u5e93\u5982dateutil\u89e3\u6790\u590d\u6742\u65e5\u671f\u683c\u5f0f\u3002<\/strong>\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u901a\u8fc7<code>datetime<\/code>\u6a21\u5757\u89e3\u6790\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u65e5\u671f\u8fdb\u884c\u683c\u5f0f\u9a8c\u8bc1\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u89e3\u6790\u5b57\u7b26\u4e32\u662f\u8f93\u5165\u65e5\u671f\u7684\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u80fd\u591f\u5904\u7406\u591a\u79cd\u65e5\u671f\u683c\u5f0f\uff0c\u5e76\u4e14\u53ef\u4ee5\u5bf9\u8f93\u5165\u7684\u65e5\u671f\u8fdb\u884c\u9a8c\u8bc1\u548c\u8f6c\u6362\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4ece<code>datetime<\/code>\u6a21\u5757\u4e2d\u5bfc\u5165<code>datetime<\/code>\u7c7b\u3002<code>datetime<\/code>\u7c7b\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u65b9\u4fbf\u5730\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662f<code>strptime()<\/code>\uff0c\u5b83\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>date_string = &quot;2023-12-31&quot;<\/p>\n<p>date_format = &quot;%Y-%m-%d&quot;<\/p>\n<p>try:<\/p>\n<p>    date_object = datetime.strptime(date_string, date_format)<\/p>\n<p>    print(&quot;\u65e5\u671f\u5bf9\u8c61:&quot;, date_object)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(&quot;\u65e5\u671f\u683c\u5f0f\u9519\u8bef:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>date_string<\/code>\u662f\u6211\u4eec\u5e0c\u671b\u89e3\u6790\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u800c<code>date_format<\/code>\u662f\u65e5\u671f\u7684\u683c\u5f0f\u8bf4\u660e\u7b26\u3002\u5728\u8c03\u7528<code>strptime()<\/code>\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u8f93\u5165\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u4e0e\u6307\u5b9a\u7684\u683c\u5f0f\u5339\u914d\uff0c\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u5426\u5219\u4f1a\u629b\u51fa<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>datetime<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python \u7684 <code>datetime<\/code> \u6a21\u5757\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\u3002\u901a\u8fc7\u5b83\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u89e3\u6790\u3001\u683c\u5f0f\u5316\u548c\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u89e3\u6790\u65e5\u671f\u5b57\u7b26\u4e32\u662f\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65e5\u671f\u8f6c\u6362\u4e3a <code>datetime<\/code> \u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002<code>datetime.strptime()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u89e3\u6790\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>input_date = input(&quot;\u8bf7\u8f93\u5165\u65e5\u671f (\u683c\u5f0f: YYYY-MM-DD): &quot;)<\/p>\n<p>try:<\/p>\n<p>    parsed_date = datetime.strptime(input_date, &quot;%Y-%m-%d&quot;)<\/p>\n<p>    print(f&quot;\u89e3\u6790\u540e\u7684\u65e5\u671f\u662f: {parsed_date}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u65e5\u671f\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u8bf7\u4f7f\u7528 YYYY-MM-DD \u683c\u5f0f\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528 <code>strptime()<\/code> \u65b9\u6cd5\u5c06\u5176\u89e3\u6790\u4e3a <code>datetime<\/code> \u5bf9\u8c61\u3002\u5982\u679c\u8f93\u5165\u7684\u65e5\u671f\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u4f1a\u629b\u51fa <code>ValueError<\/code>\uff0c\u5e76\u544a\u77e5\u7528\u6237\u683c\u5f0f\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u683c\u5f0f\u5316\u65e5\u671f\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u65e5\u671f\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7 <code>strftime()<\/code> \u65b9\u6cd5\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u683c\u5f0f\u5316\u64cd\u4f5c\u53ef\u4ee5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u66f4\u7b26\u5408\u9700\u6c42\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_date = parsed_date.strftime(&quot;%B %d, %Y&quot;)<\/p>\n<p>print(f&quot;\u683c\u5f0f\u5316\u540e\u7684\u65e5\u671f\u662f: {formatted_date}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06 <code>datetime<\/code> \u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a <code>Month Day, Year<\/code> \u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c<code>2023-12-31<\/code> \u4f1a\u88ab\u683c\u5f0f\u5316\u4e3a <code>December 31, 2023<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>dateutil<\/code> \u5e93<\/h3>\n<\/p>\n<p><p><code>dateutil<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u80fd\u591f\u5904\u7406\u66f4\u591a\u590d\u6742\u7684\u65e5\u671f\u89e3\u6790\u4efb\u52a1\u3002\u5b83\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u591a\u79cd\u65e5\u671f\u683c\u5f0f\uff0c\u800c\u65e0\u9700\u663e\u5f0f\u6307\u5b9a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u89e3\u6790\u4efb\u610f\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>dateutil.parser.parse()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u89e3\u6790\u51e0\u4e4e\u4efb\u4f55\u4eba\u7c7b\u53ef\u8bfb\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil import parser<\/p>\n<p>input_date = input(&quot;\u8bf7\u8f93\u5165\u65e5\u671f: &quot;)<\/p>\n<p>try:<\/p>\n<p>    parsed_date = parser.parse(input_date)<\/p>\n<p>    print(f&quot;\u89e3\u6790\u540e\u7684\u65e5\u671f\u662f: {parsed_date}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u89e3\u6790\u8f93\u5165\u7684\u65e5\u671f\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e <code>datetime.strptime()<\/code> \u4e0d\u540c\uff0c<code>parser.parse()<\/code> \u4e0d\u9700\u8981\u6307\u5b9a\u65e5\u671f\u683c\u5f0f\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u591a\u79cd\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u65f6\u533a\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u5728\u8bb8\u591a\u5e94\u7528\u4e2d\uff0c\u5904\u7406\u65f6\u533a\u4fe1\u606f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002Python \u7684 <code>pytz<\/code> \u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u5904\u7406\u65f6\u533a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4e3a\u65e5\u671f\u5bf9\u8c61\u6dfb\u52a0\u65f6\u533a\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>pytz<\/code> \u5e93\u4e3a\u65e5\u671f\u5bf9\u8c61\u6dfb\u52a0\u65f6\u533a\u4fe1\u606f\uff0c\u4ee5\u4fbf\u6b63\u786e\u5904\u7406\u4e0d\u540c\u5730\u533a\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<p>utc = pytz.utc<\/p>\n<p>eastern = pytz.timezone(&#39;US\/Eastern&#39;)<\/p>\n<p>n<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ve_date = datetime.strptime(&quot;2023-12-31 12:00&quot;, &quot;%Y-%m-%d %H:%M&quot;)<\/p>\n<p>localized_date = eastern.localize(naive_date)<\/p>\n<p>print(f&quot;\u4e1c\u90e8\u65f6\u95f4: {localized_date}&quot;)<\/p>\n<p>utc_date = localized_date.astimezone(utc)<\/p>\n<p>print(f&quot;UTC \u65f6\u95f4: {utc_date}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u65f6\u533a\u4fe1\u606f\u7684\u65e5\u671f\u5bf9\u8c61 (<code>naive_date<\/code>)\uff0c\u7136\u540e\u4f7f\u7528 <code>localize()<\/code> \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u4e1c\u90e8\u65f6\u95f4\u7684\u5e26\u65f6\u533a\u65e5\u671f\u5bf9\u8c61\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>astimezone()<\/code> \u65b9\u6cd5\u5c06\u4e1c\u90e8\u65f6\u95f4\u8f6c\u6362\u4e3a UTC \u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7528\u6237\u8f93\u5165\u65e5\u671f\u7684\u9a8c\u8bc1\u4e0e\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5f53\u7528\u6237\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u8f93\u5165\u65e5\u671f\u65f6\uff0c\u9a8c\u8bc1\u8f93\u5165\u7684\u6b63\u786e\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6211\u4eec\u9700\u8981\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u683c\u5f0f\u6b63\u786e\u4e14\u6709\u6548\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u8f93\u5165\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u57fa\u672c\u8f93\u5165\u9a8c\u8bc1\u6d89\u53ca\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u7b26\u5408\u9884\u671f\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def validate_date(date_string):<\/p>\n<p>    try:<\/p>\n<p>        return datetime.strptime(date_string, &quot;%Y-%m-%d&quot;)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return None<\/p>\n<p>input_date = input(&quot;\u8bf7\u8f93\u5165\u65e5\u671f (\u683c\u5f0f: YYYY-MM-DD): &quot;)<\/p>\n<p>valid_date = validate_date(input_date)<\/p>\n<p>if valid_date:<\/p>\n<p>    print(f&quot;\u6709\u6548\u65e5\u671f: {valid_date}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u65e5\u671f\u65e0\u6548\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>validate_date()<\/code> \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c1d\u8bd5\u5c06\u8f93\u5165\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a <code>datetime<\/code> \u5bf9\u8c61\u3002\u5982\u679c\u89e3\u6790\u5931\u8d25\uff0c\u5219\u8fd4\u56de <code>None<\/code>\uff0c\u5e76\u63d0\u793a\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u65e0\u6548\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u590d\u6742\u65e5\u671f\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u8fdb\u4e00\u6b65\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>date_pattern = re.compile(r&#39;\\d{4}-\\d{2}-\\d{2}&#39;)<\/p>\n<p>def validate_date_with_regex(date_string):<\/p>\n<p>    if date_pattern.fullmatch(date_string):<\/p>\n<p>        return datetime.strptime(date_string, &quot;%Y-%m-%d&quot;)<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>input_date = input(&quot;\u8bf7\u8f93\u5165\u65e5\u671f (\u683c\u5f0f: YYYY-MM-DD): &quot;)<\/p>\n<p>valid_date = validate_date_with_regex(input_date)<\/p>\n<p>if valid_date:<\/p>\n<p>    print(f&quot;\u6709\u6548\u65e5\u671f: {valid_date}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u65e5\u671f\u65e0\u6548\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u8f93\u5165\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u4e25\u683c\u7b26\u5408 <code>YYYY-MM-DD<\/code> \u683c\u5f0f\uff0c\u7136\u540e\u518d\u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u662f\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5e38\u89c1\u4efb\u52a1\u3002\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684 <code>datetime<\/code> \u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93\u5982 <code>dateutil<\/code> \u548c <code>pytz<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406\u65e5\u671f\u8f93\u5165\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u9002\u5f53\u7684\u5e93<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u65e5\u671f\u89e3\u6790\uff0c<code>datetime<\/code> \u6a21\u5757\u901a\u5e38\u5df2\u7ecf\u8db3\u591f\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u683c\u5f0f\u6216\u65f6\u533a\u5904\u7406\uff0c<code>dateutil<\/code> \u548c <code>pytz<\/code> \u662f\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9a8c\u8bc1\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u59cb\u7ec8\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u683c\u5f0f\u662f\u5426\u6b63\u786e\u3002\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7684\u53ef\u9760\u6027\uff0c\u8fd8\u80fd\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/strong>\uff1a\u5bf9\u65e5\u671f\u89e3\u6790\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u8fdb\u884c\u5904\u7406\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5f15\u5bfc\u7528\u6237\u8f93\u5165\u6b63\u786e\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong>\uff1a\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65e5\u671f\u683c\u5f0f\u8fdb\u884c\u8f93\u51fa\uff0c\u4ee5\u786e\u4fdd\u4fe1\u606f\u7684\u6e05\u6670\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u5904\u7406\u65e5\u671f\u8f93\u5165\uff0c\u5e76\u4e3a\u7528\u6237\u63d0\u4f9b\u826f\u597d\u7684\u4ea4\u4e92\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u5165\u7528\u6237\u6307\u5b9a\u7684\u65e5\u671f\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u3002\u4e3a\u4e86\u786e\u4fdd\u8f93\u5165\u7684\u65e5\u671f\u683c\u5f0f\u6b63\u786e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>strptime()<\/code>\u65b9\u6cd5\u6765\u89e3\u6790\u8f93\u5165\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u7528\u6237\u8f93\u5165\u201cYYYY-MM-DD\u201d\u683c\u5f0f\u7684\u65e5\u671f\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ndate_input = input(&quot;\u8bf7\u8f93\u5165\u65e5\u671f\uff08\u683c\u5f0f\uff1aYYYY-MM-DD\uff09: &quot;)\ntry:\n    date_object = datetime.strptime(date_input, &quot;%Y-%m-%d&quot;)\n    print(f&quot;\u60a8\u8f93\u5165\u7684\u65e5\u671f\u662f: {date_object}&quot;)\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u7684\u65e5\u671f\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u8bf7\u6309\u7167 YYYY-MM-DD \u683c\u5f0f\u8f93\u5165\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u4f1a\u88ab\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u9a8c\u8bc1\u8f93\u5165\u7684\u65e5\u671f\u662f\u5426\u6709\u6548\uff1f<\/strong><br \/>\u9a8c\u8bc1\u65e5\u671f\u6709\u6548\u6027\u662f\u4e00\u4e2a\u5173\u952e\u6b65\u9aa4\uff0c\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u662f\u5b58\u5728\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u5904\u7406\u3002\u901a\u8fc7\u5c1d\u8bd5\u89e3\u6790\u8f93\u5165\u7684\u65e5\u671f\uff0c\u5982\u679c\u89e3\u6790\u5931\u8d25\uff0c\u5219\u8bf4\u660e\u65e5\u671f\u65e0\u6548\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u89e3\u6790\u5f02\u5e38\uff0c\u5982\u679c\u8f93\u5165\u7684\u65e5\u671f\u4e0d\u7b26\u5408\u5b9e\u9645\u65e5\u671f\uff08\u59822023\u5e742\u670830\u65e5\uff09\uff0c\u5219\u4f1a\u629b\u51fa<code>ValueError<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6709\u6548\u7684\u65e5\u671f\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u65e5\u671f\u8f93\u5165\u548c\u5904\u7406\uff1f<\/strong><br \/>\u9664\u4e86<code>datetime<\/code>\u6a21\u5757\uff0cPython\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u65e5\u671f\u5904\u7406\u3002\u4f8b\u5982\uff0c<code>dateutil<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u65e5\u671f\u89e3\u6790\u529f\u80fd\uff0c\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u65e5\u671f\u683c\u5f0f\u3002\u8fd8\u6709<code>pandas<\/code>\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u65e5\u671f\u7684\u8f93\u5165\u3001\u8f6c\u6362\u548c\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>dateutil<\/code>\u5e93\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from dateutil import parser\n\ndate_input = input(&quot;\u8bf7\u8f93\u5165\u65e5\u671f: &quot;)\ntry:\n    date_object = parser.parse(date_input)\n    print(f&quot;\u60a8\u8f93\u5165\u7684\u65e5\u671f\u662f: {date_object}&quot;)\nexcept ValueError:\n    print(&quot;\u8f93\u5165\u7684\u65e5\u671f\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u5165\u65e5\u671f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528input()\u51fd\u6570\u8ba9\u7528\u6237\u624b\u52a8\u8f93\u5165\u3001\u4f7f\u7528datetime\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":994128,"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\/994110"}],"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=994110"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994110\/revisions"}],"predecessor-version":[{"id":994131,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994110\/revisions\/994131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994128"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}