{"id":1185605,"date":"2025-01-15T19:42:53","date_gmt":"2025-01-15T11:42:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1185605.html"},"modified":"2025-01-15T19:42:56","modified_gmt":"2025-01-15T11:42:56","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e6%97%b6%e9%97%b4%e6%9c%88-%e6%97%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1185605.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u65f6\u95f4\u6708 \u65e5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134700\/ae7cb4c5-ba02-4f32-a8bc-fe240cd986d9.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u65f6\u95f4\u6708 \u65e5\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u63d0\u53d6\u65f6\u95f4\u7684\u6708\u4efd\u548c\u65e5\u671f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001\u6216\u8005pandas\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528datetime\u6a21\u5757\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\uff0c\u5e76\u5c55\u793a\u5176\u5177\u4f53\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u4e2d\u7684datetime\u7c7b\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u7136\u540e\u901a\u8fc7\u76f8\u5e94\u7684\u5c5e\u6027\u83b7\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u63d0\u53d6\u6708\u4efd<\/strong><\/h2>\n<p>month = now.month<\/p>\n<h2><strong>\u63d0\u53d6\u65e5\u671f<\/strong><\/h2>\n<p>day = now.day<\/p>\n<p>print(f&quot;\u5f53\u524d\u6708\u4efd: {month}&quot;)<\/p>\n<p>print(f&quot;\u5f53\u524d\u65e5\u671f: {day}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u7684\u6708\u4efd\u548c\u65e5\u671f\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u8ba8\u8bbadatetime\u6a21\u5757\u7684\u4f7f\u7528\uff0c\u5e76\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\uff0c\u5982time\u6a21\u5757\u548cpandas\u5e93\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001DATETIME\u6a21\u5757<\/h2>\n<\/p>\n<p><h3>1\u3001\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528datetime\u6a21\u5757\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u3002<code>datetime.now()<\/code>\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>print(now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u83b7\u53d6\u5230\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7<code>.month<\/code>\u548c<code>.day<\/code>\u5c5e\u6027\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">month = now.month<\/p>\n<p>day = now.day<\/p>\n<p>print(f&quot;\u6708\u4efd: {month}&quot;)<\/p>\n<p>print(f&quot;\u65e5\u671f: {day}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u201cYYYY-MM-DD HH:MM:SS\u201d\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_date = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u5904\u7406\u5b57\u7b26\u4e32\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u65e5\u671f\u548c\u65f6\u95f4\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u7ed9\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u7136\u540e\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_str = &quot;2023-10-25 14:30:00&quot;<\/p>\n<p>date_obj = datetime.strptime(date_str, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>month = date_obj.month<\/p>\n<p>day = date_obj.day<\/p>\n<p>print(f&quot;\u6708\u4efd: {month}&quot;)<\/p>\n<p>print(f&quot;\u65e5\u671f: {day}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001TIME\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u867d\u7136datetime\u6a21\u5757\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u9996\u9009\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528time\u6a21\u5757\u4e5f\u53ef\u4ee5\u5b8c\u6210\u4efb\u52a1\u3002time\u6a21\u5757\u63d0\u4f9b\u4e86\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u51fd\u6570\uff0c\u4ee5\u53ca\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>time.localtime()<\/code>\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u672c\u5730\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>current_time = time.localtime()<\/p>\n<p>print(current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u83b7\u53d6\u5230\u5f53\u524d\u7684\u672c\u5730\u65f6\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7<code>tm_mon<\/code>\u548c<code>tm_mday<\/code>\u5c5e\u6027\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">month = current_time.tm_mon<\/p>\n<p>day = current_time.tm_mday<\/p>\n<p>print(f&quot;\u6708\u4efd: {month}&quot;)<\/p>\n<p>print(f&quot;\u65e5\u671f: {day}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>time.strftime<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_date = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, current_time)<\/p>\n<p>print(formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u5904\u7406\u5b57\u7b26\u4e32\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u65e5\u671f\u548c\u65f6\u95f4\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u7ed9\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3atime\u7ed3\u6784\uff0c\u7136\u540e\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_str = &quot;2023-10-25 14:30:00&quot;<\/p>\n<p>date_obj = time.strptime(date_str, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>month = date_obj.tm_mon<\/p>\n<p>day = date_obj.tm_mday<\/p>\n<p>print(f&quot;\u6708\u4efd: {month}&quot;)<\/p>\n<p>print(f&quot;\u65e5\u671f: {day}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001PANDAS\u5e93<\/h2>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u6570\u636e\u5206\u6790\u7684\u5229\u5668\uff0c\u5b83\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u4fbf\u6377\u65b9\u6cd5\u3002Pandas\u4e2d\u7684<code>pd.to_datetime<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u5e76\u4e14\u53ef\u4ee5\u76f4\u63a5\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5bfc\u5165Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165Pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u5b57\u7b26\u4e32\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>pd.to_datetime<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7<code>.month<\/code>\u548c<code>.day<\/code>\u5c5e\u6027\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_str = &quot;2023-10-25 14:30:00&quot;<\/p>\n<p>date_obj = pd.to_datetime(date_str)<\/p>\n<p>month = date_obj.month<\/p>\n<p>day = date_obj.day<\/p>\n<p>print(f&quot;\u6708\u4efd: {month}&quot;)<\/p>\n<p>print(f&quot;\u65e5\u671f: {day}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5904\u7406Pandas DataFrame\u4e2d\u7684\u65e5\u671f<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u65e5\u671f\u548c\u65f6\u95f4\u6570\u636e\u5b58\u50a8\u5728Pandas DataFrame\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pd.to_datetime<\/code>\u51fd\u6570\u5c06\u65e5\u671f\u5217\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7<code>.dt<\/code>\u5c5e\u6027\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&quot;date&quot;: [&quot;2023-10-25&quot;, &quot;2023-11-15&quot;, &quot;2023-12-05&quot;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&quot;date&quot;] = pd.to_datetime(df[&quot;date&quot;])<\/p>\n<p>df[&quot;month&quot;] = df[&quot;date&quot;].dt.month<\/p>\n<p>df[&quot;day&quot;] = df[&quot;date&quot;].dt.day<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u65e5\u671f\u548c\u65f6\u95f4\u4e2d\u7684\u6708\u4efd\u548c\u65e5\u671f\u3002\u65e0\u8bba\u662f\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u8fd8\u662fPandas\u5e93\uff0c\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u7efc\u5408\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u4e00\u4e2a\u9879\u76ee\u4e2d\u4f7f\u7528datetime\u6a21\u5757\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u65e5\u671f\u5b57\u7b26\u4e32\u5217\u8868<\/strong><\/h2>\n<p>date_strings = [&quot;2023-10-25 14:30:00&quot;, &quot;2023-11-15 09:45:00&quot;, &quot;2023-12-05 18:20:00&quot;]<\/p>\n<h2><strong>\u521b\u5efa\u7a7a\u5217\u8868\u6765\u5b58\u50a8\u6708\u4efd\u548c\u65e5\u671f<\/strong><\/h2>\n<p>months = []<\/p>\n<p>days = []<\/p>\n<h2><strong>\u904d\u5386\u65e5\u671f\u5b57\u7b26\u4e32\u5217\u8868<\/strong><\/h2>\n<p>for date_str in date_strings:<\/p>\n<p>    # \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/p>\n<p>    date_obj = datetime.strptime(date_str, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>    # \u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/p>\n<p>    months.append(date_obj.month)<\/p>\n<p>    days.append(date_obj.day)<\/p>\n<h2><strong>\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&quot;\u6708\u4efd\u5217\u8868: {months}&quot;)<\/p>\n<p>print(f&quot;\u65e5\u671f\u5217\u8868: {days}&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\u65e5\u671f\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26\u4e32\uff0c\u5c06\u5176\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u5e76\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002\u6700\u540e\uff0c\u5c06\u63d0\u53d6\u7684\u6708\u4efd\u548c\u65e5\u671f\u5b58\u50a8\u5230\u5217\u8868\u4e2d\u5e76\u6253\u5370\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5176\u4ed6\u76f8\u5173\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\uff0cdatetime\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u8ba1\u7b97\u65e5\u671f\u5dee\u3001\u589e\u52a0\u6216\u51cf\u5c11\u5929\u6570\u3001\u5224\u65ad\u95f0\u5e74\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8ba1\u7b97\u65e5\u671f\u5dee<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>date1 = datetime(2023, 10, 25)<\/p>\n<p>date2 = datetime(2023, 12, 5)<\/p>\n<h2><strong>\u8ba1\u7b97\u65e5\u671f\u5dee<\/strong><\/h2>\n<p>delta = date2 - date1<\/p>\n<p>print(f&quot;\u65e5\u671f\u5dee: {delta.days} \u5929&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u589e\u52a0\u6216\u51cf\u5c11\u5929\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528timedelta\u5bf9\u8c61\u589e\u52a0\u6216\u51cf\u5c11\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime, timedelta<\/p>\n<p>date = datetime(2023, 10, 25)<\/p>\n<h2><strong>\u589e\u52a010\u5929<\/strong><\/h2>\n<p>new_date = date + timedelta(days=10)<\/p>\n<p>print(f&quot;\u589e\u52a010\u5929\u540e\u7684\u65e5\u671f: {new_date}&quot;)<\/p>\n<h2><strong>\u51cf\u5c115\u5929<\/strong><\/h2>\n<p>new_date = date - timedelta(days=5)<\/p>\n<p>print(f&quot;\u51cf\u5c115\u5929\u540e\u7684\u65e5\u671f: {new_date}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5224\u65ad\u95f0\u5e74<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528calendar\u6a21\u5757\u5224\u65ad\u67d0\u4e00\u5e74\u662f\u5426\u4e3a\u95f0\u5e74\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<p>year = 2024<\/p>\n<p>is_leap = calendar.isleap(year)<\/p>\n<p>print(f&quot;{year} \u662f\u5426\u662f\u95f0\u5e74: {is_leap}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u53ef\u4ee5\u66f4\u5168\u9762\u5730\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Python\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u9ad8\u6548\u5730\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u76f8\u5173\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u65e5\u671f\u7684\u6708\u4efd\u548c\u65e5\u671f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u901a\u8fc7\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7<code>month<\/code>\u548c<code>day<\/code>\u5c5e\u6027\u83b7\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u65e5\u671f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08<code>re<\/code>\u6a21\u5757\uff09\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u65e5\u671f\u3002\u901a\u8fc7\u7f16\u5199\u5408\u9002\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5339\u914d\u65e5\u671f\u683c\u5f0f\uff08\u5982MM\/DD\/YYYY\u6216YYYY-MM-DD\uff09\uff0c\u7136\u540e\u5c06\u63d0\u53d6\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u4ee5\u83b7\u53d6\u6708\u4efd\u548c\u65e5\u671f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0d\u540c\u65f6\u95f4\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>Python\u7684<code>dateutil<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u81ea\u52a8\u89e3\u6790\u591a\u79cd\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002\u4f7f\u7528<code>dateutil.parser.parse()<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u65e5\u671f\u683c\u5f0f\uff0c\u65e0\u9700\u624b\u52a8\u6307\u5b9a\u683c\u5f0f\u3002\u89e3\u6790\u540e\uff0c\u60a8\u540c\u6837\u53ef\u4ee5\u901a\u8fc7<code>month<\/code>\u548c<code>day<\/code>\u5c5e\u6027\u83b7\u53d6\u6240\u9700\u7684\u6708\u4efd\u548c\u65e5\u671f\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63d0\u53d6\u65f6\u95f4\u7684\u6708\u4efd\u548c\u65e5\u671f\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001\u6216\u8005pandas\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1185614,"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\/1185605"}],"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=1185605"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185605\/revisions"}],"predecessor-version":[{"id":1185616,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185605\/revisions\/1185616"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1185614"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1185605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1185605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1185605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}