{"id":1090133,"date":"2025-01-08T13:59:55","date_gmt":"2025-01-08T05:59:55","guid":{"rendered":""},"modified":"2025-01-08T13:59:57","modified_gmt":"2025-01-08T05:59:57","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%98%af%e5%90%a6%e6%98%af%e6%98%9f%e6%9c%9f%e4%ba%94-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1090133.html","title":{"rendered":"Python\u5982\u4f55\u5224\u65ad\u662f\u5426\u662f\u661f\u671f\u4e94"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202804\/6c8578ce-31c8-4f6d-979e-8ecc7bda5e09.webp\" alt=\"Python\u5982\u4f55\u5224\u65ad\u662f\u5426\u662f\u661f\u671f\u4e94\" \/><\/p>\n<p><p> <strong>Python\u5224\u65ad\u662f\u5426\u662f\u661f\u671f\u4e94\u7684\u6838\u5fc3\u65b9\u6cd5\u662f\u4f7f\u7528 datetime \u6a21\u5757\u7684\u529f\u80fd\uff0c\u901a\u8fc7\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u5e76\u5224\u65ad\u8be5\u65e5\u671f\u7684\u661f\u671f\u6570\u503c\u6765\u5b9e\u73b0\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165 datetime \u6a21\u5757\uff0c\u5e76\u83b7\u53d6\u5f53\u524d\u65e5\u671f<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528 Python \u5224\u65ad\u67d0\u4e00\u5929\u662f\u5426\u662f\u661f\u671f\u4e94\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165 datetime \u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u76f8\u5173\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7 datetime.datetime.today() \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>today = datetime.datetime.today()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u83b7\u53d6\u661f\u671f\u6570\u503c<\/p>\n<\/p>\n<p><p>datetime \u6a21\u5757\u7684 weekday() \u65b9\u6cd5\u8fd4\u56de\u7684\u503c\u662f 0\uff08\u661f\u671f\u4e00\uff09\u5230 6\uff08\u661f\u671f\u65e5\uff09\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5224\u65ad\u8fd4\u56de\u503c\u662f\u5426\u4e3a 4 \u6765\u5224\u65ad\u662f\u5426\u662f\u661f\u671f\u4e94\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">is_friday = today.weekday() == 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u793a\u4f8b\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5224\u65ad\u5f53\u524d\u65e5\u671f\u662f\u5426\u662f\u661f\u671f\u4e94\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>def is_today_friday():<\/p>\n<p>    today = datetime.datetime.today()<\/p>\n<p>    return today.weekday() == 4<\/p>\n<p>if is_today_friday():<\/p>\n<p>    print(&quot;\u4eca\u5929\u662f\u661f\u671f\u4e94\uff01&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u4eca\u5929\u4e0d\u662f\u661f\u671f\u4e94\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6269\u5c55\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5224\u65ad\u5f53\u524d\u65e5\u671f\u662f\u5426\u662f\u661f\u671f\u4e94\uff0c\u4f60\u8fd8\u53ef\u4ee5\u6269\u5c55\u8fd9\u4e2a\u529f\u80fd\u6765\u5224\u65ad\u5176\u4ed6\u65e5\u671f\u662f\u5426\u662f\u661f\u671f\u4e94\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 datetime.datetime(year, month, day) \u521b\u5efa\u4efb\u610f\u65e5\u671f\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528 weekday() \u65b9\u6cd5\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_specific_date_friday(year, month, day):<\/p>\n<p>    date = datetime.datetime(year, month, day)<\/p>\n<p>    return date.weekday() == 4<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>if is_specific_date_friday(2023, 10, 6):<\/p>\n<p>    print(&quot;2023\u5e7410\u67086\u65e5\u662f\u661f\u671f\u4e94\uff01&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;2023\u5e7410\u67086\u65e5\u4e0d\u662f\u661f\u671f\u4e94\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5224\u65ad\u65e5\u671f\u662f\u5426\u662f\u661f\u671f\u4e94\u7684\u529f\u80fd\u5728\u5f88\u591a\u573a\u666f\u4e2d\u90fd\u6709\u5e94\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8ba1\u5212\u4efb\u52a1<\/strong>\uff1a\u5728\u81ea\u52a8\u5316\u811a\u672c\u4e2d\uff0c\u5b9a\u671f\u68c0\u67e5\u67d0\u4e9b\u4efb\u52a1\u662f\u5426\u9700\u8981\u5728\u661f\u671f\u4e94\u6267\u884c\u3002<\/li>\n<li><strong>\u65e5\u5fd7\u5206\u6790<\/strong>\uff1a\u5728\u65e5\u5fd7\u5206\u6790\u4e2d\uff0c\u68c0\u67e5\u67d0\u4e9b\u7279\u5b9a\u4e8b\u4ef6\u662f\u5426\u53d1\u751f\u5728\u661f\u671f\u4e94\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u62a5\u544a\u3002<\/li>\n<li><strong>\u91d1\u878d\u4ea4\u6613<\/strong>\uff1a\u5728\u91d1\u878d\u9886\u57df\uff0c\u6839\u636e\u5e02\u573a\u89c4\u5f8b\uff0c\u67d0\u4e9b\u4ea4\u6613\u7b56\u7565\u9700\u8981\u5728\u661f\u671f\u4e94\u8fdb\u884c\u8c03\u6574\u548c\u6267\u884c\u3002<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython \u53ef\u4ee5\u65b9\u4fbf\u5730\u5224\u65ad\u67d0\u4e00\u5929\u662f\u5426\u662f\u661f\u671f\u4e94\u3002\u4f7f\u7528 datetime \u6a21\u5757\u7684\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u548c\u64cd\u4f5c\u65e5\u671f\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u65e5\u671f\u8ba1\u7b97\u548c\u5224\u65ad\u9700\u6c42\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6df1\u5165\u7406\u89e3 datetime \u6a21\u5757<\/h3>\n<\/p>\n<p><p>datetime \u6a21\u5757\u662f Python \u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5404\u79cd\u529f\u80fd\u3002\u901a\u8fc7\u6df1\u5165\u7406\u89e3\u548c\u638c\u63e1 datetime \u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u548c\u64cd\u4f5c\u65e5\u671f\u65f6\u95f4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. datetime \u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>datetime \u6a21\u5757\u7684\u6838\u5fc3\u662f datetime \u5bf9\u8c61\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u5177\u4f53\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u901a\u8fc7 datetime.datetime.now() \u6216 datetime.datetime.today() \u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.datetime.now()<\/p>\n<p>print(f&quot;\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff1a{now}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. timedelta \u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>timedelta \u5bf9\u8c61\u8868\u793a\u4e24\u4e2a datetime \u5bf9\u8c61\u4e4b\u95f4\u7684\u65f6\u95f4\u5dee\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 timedelta \u5bf9\u8c61\u8fdb\u884c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u52a0\u51cf\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.datetime.now()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u8868\u793a1\u5929\u7684 timedelta \u5bf9\u8c61<\/strong><\/h2>\n<p>one_day = datetime.timedelta(days=1)<\/p>\n<h2><strong>\u8ba1\u7b97\u6628\u5929\u7684\u65e5\u671f<\/strong><\/h2>\n<p>yesterday = now - one_day<\/p>\n<p>print(f&quot;\u6628\u5929\u7684\u65e5\u671f\uff1a{yesterday}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. strftime \u548c strptime \u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>strftime \u65b9\u6cd5\u7528\u4e8e\u5c06 datetime \u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0cstrptime \u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a datetime \u5bf9\u8c61\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5728\u65e5\u671f\u65f6\u95f4\u7684\u683c\u5f0f\u5316\u548c\u89e3\u6790\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.datetime.now()<\/p>\n<h2><strong>\u5c06 datetime \u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>formatted_date = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(f&quot;\u683c\u5f0f\u5316\u65e5\u671f\uff1a{formatted_date}&quot;)<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a datetime \u5bf9\u8c61<\/strong><\/h2>\n<p>parsed_date = datetime.datetime.strptime(formatted_date, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(f&quot;\u89e3\u6790\u540e\u7684\u65e5\u671f\uff1a{parsed_date}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1 datetime \u6a21\u5757\u7684\u8fd9\u4e9b\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u5730\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u6570\u636e\uff0c\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u65e5\u671f\u8ba1\u7b97\u548c\u5224\u65ad\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u65e5\u671f\u8ba1\u7b97\u548c\u5224\u65ad\u5f80\u5f80\u9700\u8981\u66f4\u52a0\u590d\u6742\u7684\u903b\u8f91\u548c\u529f\u80fd\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\u573a\u666f\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h4>1. \u5224\u65ad\u5de5\u4f5c\u65e5\u548c\u5468\u672b<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5224\u65ad\u662f\u5426\u662f\u661f\u671f\u4e94\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5224\u65ad\u67d0\u4e00\u5929\u662f\u5426\u662f\u5de5\u4f5c\u65e5\u6216\u5468\u672b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>def is_weekday(date):<\/p>\n<p>    # weekday() \u8fd4\u56de0\uff08\u661f\u671f\u4e00\uff09\u52306\uff08\u661f\u671f\u65e5\uff09<\/p>\n<p>    return date.weekday() &lt; 5<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>today = datetime.datetime.today()<\/p>\n<p>if is_weekday(today):<\/p>\n<p>    print(&quot;\u4eca\u5929\u662f\u5de5\u4f5c\u65e5\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u4eca\u5929\u662f\u5468\u672b\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee<\/h4>\n<\/p>\n<p><p>\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\u662f\u65e5\u671f\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u9700\u6c42\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 timedelta \u5bf9\u8c61\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>def days_between_dates(date1, date2):<\/p>\n<p>    delta = date2 - date1<\/p>\n<p>    return delta.days<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>date1 = datetime.datetime(2023, 1, 1)<\/p>\n<p>date2 = datetime.datetime(2023, 10, 1)<\/p>\n<p>days_diff = days_between_dates(date1, date2)<\/p>\n<p>print(f&quot;\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570\u5dee\uff1a{days_diff} \u5929&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5224\u65ad\u95f0\u5e74<\/h4>\n<\/p>\n<p><p>\u5224\u65ad\u67d0\u4e00\u5e74\u662f\u5426\u662f\u95f0\u5e74\u4e5f\u662f\u65e5\u671f\u5904\u7406\u4e2d\u7684\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 calendar \u6a21\u5757\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<p>def is_leap_year(year):<\/p>\n<p>    return calendar.isleap(year)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>year = 2023<\/p>\n<p>if is_leap_year(year):<\/p>\n<p>    print(f&quot;{year} \u5e74\u662f\u95f0\u5e74\u3002&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{year} \u5e74\u4e0d\u662f\u95f0\u5e74\u3002&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3\u4e0e\u5c55\u671b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528 Python \u5224\u65ad\u67d0\u4e00\u5929\u662f\u5426\u662f\u661f\u671f\u4e94\uff0c\u5e76\u638c\u63e1\u4e86 datetime \u6a21\u5757\u7684\u57fa\u672c\u7528\u6cd5\u548c\u9ad8\u7ea7\u5e94\u7528\u6280\u5de7\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u6570\u636e\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><p>\u968f\u7740\u6280\u672f\u7684\u53d1\u5c55\u548c\u5e94\u7528\u7684\u4e0d\u65ad\u6df1\u5165\uff0c\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u7684\u9700\u6c42\u4e5f\u4f1a\u8d8a\u6765\u8d8a\u591a\u6837\u5316\u548c\u590d\u6742\u5316\u3002\u638c\u63e1\u548c\u6df1\u5165\u7406\u89e3 datetime \u6a21\u5757\u7684\u5404\u79cd\u529f\u80fd\u548c\u5e94\u7528\u573a\u666f\uff0c\u5c06\u5e2e\u52a9\u4f60\u5728\u672a\u6765\u7684\u5de5\u4f5c\u548c\u5b66\u4e60\u4e2d\u66f4\u52a0\u6e38\u5203\u6709\u4f59\u5730\u5e94\u5bf9\u5404\u79cd\u65e5\u671f\u65f6\u95f4\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u7684\u661f\u671f\u51e0\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u7684\u661f\u671f\u51e0\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import datetime\n\n# \u83b7\u53d6\u5f53\u524d\u65e5\u671f\ntoday = datetime.datetime.now()\n\n# \u83b7\u53d6\u661f\u671f\u51e0\uff080=\u661f\u671f\u4e00\uff0c1=\u661f\u671f\u4e8c\uff0c..., 6=\u661f\u671f\u65e5\uff09\nweekday = today.weekday()\n\nprint(f&quot;\u4eca\u5929\u662f\u661f\u671f{weekday + 1}&quot;)  # \u8f93\u51fa\u661f\u671f\u51e0\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u5bf9\u5e94\u7684\u661f\u671f\u51e0\uff0c\u65b9\u4fbf\u60a8\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u7279\u5b9a\u65e5\u671f\u662f\u5426\u4e3a\u661f\u671f\u4e94\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u7279\u5b9a\u65e5\u671f\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u68c0\u67e5\u8be5\u65e5\u671f\u7684\u661f\u671f\u51e0\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import datetime\n\n# \u521b\u5efa\u4e00\u4e2a\u7279\u5b9a\u65e5\u671f\u5bf9\u8c61\nspecific_date = datetime.datetime(2023, 10, 6)  # \u4f8b\u5982\uff1a2023\u5e7410\u67086\u65e5\n\n# \u5224\u65ad\u662f\u5426\u4e3a\u661f\u671f\u4e94\nif specific_date.weekday() == 4:  # 4\u4ee3\u8868\u661f\u671f\u4e94\n    print(&quot;\u8be5\u65e5\u671f\u662f\u661f\u671f\u4e94&quot;)\nelse:\n    print(&quot;\u8be5\u65e5\u671f\u4e0d\u662f\u661f\u671f\u4e94&quot;)\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5224\u65ad\u4efb\u4f55\u65e5\u671f\u662f\u5426\u4e3a\u661f\u671f\u4e94\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\u5e76\u5224\u65ad\u662f\u5426\u4e3a\u661f\u671f\u4e94\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\uff0c\u5e76\u4f7f\u7528<code>strptime()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import datetime\n\n# \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u65e5\u671f\nuser_input = input(&quot;\u8bf7\u8f93\u5165\u65e5\u671f\uff08\u683c\u5f0f\uff1aYYYY-MM-DD\uff09\uff1a&quot;)\ndate_format = &quot;%Y-%m-%d&quot;\n\ntry:\n    # \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\n    user_date = datetime.datetime.strptime(user_input, date_format)\n    \n    # \u5224\u65ad\u662f\u5426\u4e3a\u661f\u671f\u4e94\n    if user_date.weekday() == 4:\n        print(&quot;\u8f93\u5165\u7684\u65e5\u671f\u662f\u661f\u671f\u4e94&quot;)\n    else:\n        print(&quot;\u8f93\u5165\u7684\u65e5\u671f\u4e0d\u662f\u661f\u671f\u4e94&quot;)\nexcept ValueError:\n    print(&quot;\u65e5\u671f\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u8bf7\u4f7f\u7528YYYY-MM-DD\u683c\u5f0f&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u80fd\u5224\u65ad\u661f\u671f\u51e0\uff0c\u8fd8\u80fd\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u65ad\u662f\u5426\u662f\u661f\u671f\u4e94\u7684\u6838\u5fc3\u65b9\u6cd5\u662f\u4f7f\u7528 datetime \u6a21\u5757\u7684\u529f\u80fd\uff0c\u901a\u8fc7\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u5e76\u5224\u65ad\u8be5\u65e5\u671f\u7684\u661f\u671f [&hellip;]","protected":false},"author":3,"featured_media":1090142,"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\/1090133"}],"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=1090133"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090133\/revisions"}],"predecessor-version":[{"id":1090143,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090133\/revisions\/1090143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1090142"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1090133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1090133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1090133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}