{"id":1007381,"date":"2024-12-27T10:53:00","date_gmt":"2024-12-27T02:53:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007381.html"},"modified":"2024-12-27T10:53:09","modified_gmt":"2024-12-27T02:53:09","slug":"%e6%97%b6%e9%97%b4%e4%bb%a3%e7%a0%81%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007381.html","title":{"rendered":"\u65f6\u95f4\u4ee3\u7801\u5982\u4f55\u7f16\u5199Python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083124\/f5cadcb8-fc85-4d7d-875a-68cc3fdf079d.webp\" alt=\"\u65f6\u95f4\u4ee3\u7801\u5982\u4f55\u7f16\u5199Python\" \/><\/p>\n<p><p> <strong>\u7f16\u5199Python\u65f6\u95f4\u4ee3\u7801\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001calendar\u6a21\u5757\u7b49\u3002datetime\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u65e5\u671f\u548c\u65f6\u95f4\u7684\u591a\u79cd\u64cd\u4f5c\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3001\u8ba1\u7b97\u65f6\u95f4\u5dee\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u7b49\u529f\u80fd\u3002<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406\u65f6\u95f4\u548c\u65e5\u671f\u7684\u4efb\u52a1\u4e3b\u8981\u4f9d\u8d56\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u6a21\u5757\uff1adatetime\u3001time\u548ccalendar\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7c7b\uff0c\u6bd4\u5982datetime\u3001date\u3001time\u3001timedelta\u7b49\u3002time\u6a21\u5757\u5219\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u4efb\u52a1\uff0c\u5982\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u3001\u6682\u505c\u7a0b\u5e8f\u6267\u884c\u7b49\u3002calendar\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u4e0e\u65e5\u5386\u76f8\u5173\u7684\u4efb\u52a1\uff0c\u5982\u83b7\u53d6\u67d0\u4e2a\u6708\u7684\u65e5\u5386\u7b49\u3002\u4ee5\u4e0b\u662f\u5bf9\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><p><strong>datetime\u6a21\u5757\u7684\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u548c\u65b9\u6cd5\u6765\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528datetime\u6a21\u5757\u7684\u57fa\u672c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>current_time = datetime.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\uff1a&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">formatted_time = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u540e\u7684\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u65e5\u671f\u548c\u65f6\u95f4\u7684\u89e3\u6790\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">date_string = &quot;2023-10-25 14:30:00&quot;<\/p>\n<p>parsed_date = datetime.strptime(date_string, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u89e3\u6790\u540e\u7684\u65e5\u671f\uff1a&quot;, parsed_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u65e5\u671f\u7684\u52a0\u51cf\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import timedelta<\/p>\n<p>future_date = current_time + timedelta(days=5)<\/p>\n<p>print(&quot;\u4e94\u5929\u540e\u7684\u65e5\u671f\uff1a&quot;, future_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7datetime\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b8c\u6210\u5404\u79cd\u65e5\u671f\u548c\u65f6\u95f4\u7684\u64cd\u4f5c\uff0c\u4e0b\u9762\u5c06\u5bf9Python\u4e2d\u65f6\u95f4\u5904\u7406\u7684\u5176\u4ed6\u65b9\u9762\u8fdb\u884c\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001DATETIME\u6a21\u5757<\/strong><\/h2>\n<p><p>datetime\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6838\u5fc3\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u4e2a\u7c7b\u548c\u65b9\u6cd5\u6765\u5e2e\u52a9\u6211\u4eec\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>1. \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u662fdatetime\u6a21\u5757\u4e2d\u6700\u57fa\u672c\u7684\u64cd\u4f5c\u4e4b\u4e00\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>datetime.now()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u7684datetime\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>current_datetime = datetime.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, current_datetime)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>datetime.today()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4e0e<code>datetime.now()<\/code>\u7c7b\u4f3c\uff0c\u4e5f\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">current_date = datetime.today()<\/p>\n<p>print(&quot;\u5f53\u524d\u65e5\u671f\uff1a&quot;, current_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u65e5\u671f\u548c\u65f6\u95f4\u7684\u683c\u5f0f\u5316<\/h2>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u662f\u6307\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86<code>strftime()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<code>strftime()<\/code>\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u5b57\u7b26\u4e32\u5b9a\u4e49\u4e86\u65e5\u671f\u548c\u65f6\u95f4\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_date = current_datetime.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4e2d\u7684\u5404\u4e2a\u7b26\u53f7\u4ee3\u8868\u4e0d\u540c\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5143\u7d20\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>%Y<\/code>\uff1a\u5e74\u4efd\uff08\u56db\u4f4d\u6570\uff09<\/li>\n<li><code>%m<\/code>\uff1a\u6708\u4efd\uff08\u4e24\u4f4d\u6570\uff09<\/li>\n<li><code>%d<\/code>\uff1a\u65e5\u671f\uff08\u4e24\u4f4d\u6570\uff09<\/li>\n<li><code>%H<\/code>\uff1a\u5c0f\u65f6\uff0824\u5c0f\u65f6\u5236\uff09<\/li>\n<li><code>%M<\/code>\uff1a\u5206\u949f<\/li>\n<li><code>%S<\/code>\uff1a\u79d2<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7ec4\u5408\u8fd9\u4e9b\u7b26\u53f7\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5404\u79cd\u683c\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>3. \u65e5\u671f\u548c\u65f6\u95f4\u7684\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u89e3\u6790\u65e5\u671f\u548c\u65f6\u95f4\u662f\u6307\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86<code>strptime()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<code>strptime()<\/code>\u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u89e3\u6790\u7684\u5b57\u7b26\u4e32\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">date_string = &quot;2023-10-25 14:30:00&quot;<\/p>\n<p>parsed_datetime = datetime.strptime(date_string, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u89e3\u6790\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, parsed_datetime)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u542b\u4e49\u4e0e<code>strftime()<\/code>\u65b9\u6cd5\u4e2d\u7684\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h2>4. \u65e5\u671f\u548c\u65f6\u95f4\u7684\u52a0\u51cf<\/h2>\n<\/p>\n<p><p>datetime\u6a21\u5757\u4e2d\u7684<code>timedelta<\/code>\u7c7b\u7528\u4e8e\u8868\u793a\u65f6\u95f4\u95f4\u9694\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>timedelta<\/code>\u5bf9\u8c61\u6765\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u8fdb\u884c\u52a0\u51cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import timedelta<\/p>\n<p>future_datetime = current_datetime + timedelta(days=5)<\/p>\n<p>print(&quot;\u4e94\u5929\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, future_datetime)<\/p>\n<p>past_datetime = current_datetime - timedelta(weeks=2)<\/p>\n<p>print(&quot;\u4e24\u5468\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, past_datetime)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>timedelta<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u52a0\u51cf\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001TIME\u6a21\u5757<\/strong><\/h2>\n<p><p>time\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65f6\u95f4\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u6a21\u5757\u3002\u4e0edatetime\u6a21\u5757\u4e0d\u540c\uff0ctime\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u4efb\u52a1\uff0c\u5982\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u3001\u6682\u505c\u7a0b\u5e8f\u6267\u884c\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/h2>\n<\/p>\n<p><p>\u65f6\u95f4\u6233\u662f\u6307\u81ea1970\u5e741\u67081\u65e5\uff08UTC\uff09\u4ee5\u6765\u7684\u79d2\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>time.time()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>current_timestamp = time.time()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\u6233\uff1a&quot;, current_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u65f6\u95f4\u6233\u901a\u5e38\u7528\u4e8e\u8bb0\u5f55\u67d0\u4e00\u65f6\u523b\u7684\u7cbe\u786e\u65f6\u95f4\u6216\u8fdb\u884c\u65f6\u95f4\u5dee\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h2>2. \u683c\u5f0f\u5316\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u4e0edatetime\u6a21\u5757\u7c7b\u4f3c\uff0ctime\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u683c\u5f0f\u5316\u65f6\u95f4\u7684\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>time.strftime()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time.localtime())<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u540e\u7684\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>time.strftime()<\/code>\u65b9\u6cd5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u65f6\u95f4\u5bf9\u8c61\u3002\u65f6\u95f4\u5bf9\u8c61\u901a\u5e38\u7531<code>time.localtime()<\/code>\u6216<code>time.gmtime()<\/code>\u65b9\u6cd5\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h2>3. \u7a0b\u5e8f\u6682\u505c\u6267\u884c<\/h2>\n<\/p>\n<p><p>time\u6a21\u5757\u63d0\u4f9b\u4e86<code>time.sleep()<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\u3002\u8be5\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u6682\u505c\u7684\u79d2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;\u7a0b\u5e8f\u6682\u505c2\u79d2&quot;)<\/p>\n<p>time.sleep(2)<\/p>\n<p>print(&quot;\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>time.sleep()<\/code>\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u901f\u5ea6\u6216\u5b9e\u73b0\u67d0\u4e9b\u5b9a\u65f6\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001CALENDAR\u6a21\u5757<\/strong><\/h2>\n<p><p>calendar\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u5386\u76f8\u5173\u4efb\u52a1\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u751f\u6210\u65e5\u5386\u3001\u68c0\u67e5\u95f0\u5e74\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1. \u751f\u6210\u65e5\u5386<\/h2>\n<\/p>\n<p><p>calendar\u6a21\u5757\u63d0\u4f9b\u4e86<code>calendar.monthcalendar()<\/code>\u65b9\u6cd5\u6765\u751f\u6210\u67d0\u4e2a\u6708\u7684\u65e5\u5386\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5b50\u5217\u8868\u8868\u793a\u4e00\u5468\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<p>year = 2023<\/p>\n<p>month = 10<\/p>\n<p>month_calendar = calendar.monthcalendar(year, month)<\/p>\n<p>print(f&quot;{year}\u5e74{month}\u6708\u7684\u65e5\u5386\uff1a&quot;)<\/p>\n<p>for week in month_calendar:<\/p>\n<p>    print(week)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>calendar.monthcalendar()<\/code>\u65b9\u6cd5\u63a5\u6536\u5e74\u4efd\u548c\u6708\u4efd\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u6708\u7684\u65e5\u5386\u3002<\/p>\n<\/p>\n<p><h2>2. \u68c0\u67e5\u95f0\u5e74<\/h2>\n<\/p>\n<p><p>calendar\u6a21\u5757\u63d0\u4f9b\u4e86<code>calendar.isleap()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u67d0\u4e00\u5e74\u662f\u5426\u4e3a\u95f0\u5e74\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">year = 2024<\/p>\n<p>is_leap = calendar.isleap(year)<\/p>\n<p>print(f&quot;{year}\u5e74\u662f\u95f0\u5e74\uff1a&quot;, is_leap)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>calendar.isleap()<\/code>\u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u5e74\u4efd\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u5e74\u662f\u5426\u4e3a\u95f0\u5e74\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406\u65f6\u95f4\u548c\u65e5\u671f\u7684\u4efb\u52a1\u4e3b\u8981\u4f9d\u8d56\u4e8edatetime\u3001time\u548ccalendar\u6a21\u5757\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u548c\u65b9\u6cd5\u6765\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\uff0c\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u9996\u9009\u6a21\u5757\u3002time\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u4efb\u52a1\uff0c\u5982\u83b7\u53d6\u65f6\u95f4\u6233\u3001\u6682\u505c\u7a0b\u5e8f\u6267\u884c\u7b49\u3002calendar\u6a21\u5757\u5219\u7528\u4e8e\u5904\u7406\u4e0e\u65e5\u5386\u76f8\u5173\u7684\u4efb\u52a1\uff0c\u5982\u751f\u6210\u65e5\u5386\u3001\u68c0\u67e5\u95f0\u5e74\u7b49\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b8c\u6210\u5404\u79cd\u65f6\u95f4\u548c\u65e5\u671f\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u65f6\u95f4\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u65f6\u95f4\u4ee3\u7801\u901a\u5e38\u4f7f\u7528\u5185\u7f6e\u7684<code>datetime<\/code>\u6a21\u5757\u3002\u53ef\u4ee5\u901a\u8fc7<code>datetime.datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5e76\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>now().strftime(&#39;%Y-%m-%d %H:%M:%S&#39;)<\/code>\u53ef\u4ee5\u5f97\u5230\u201c2023-10-03 14:30:00\u201d\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>Python\u4e2d\u65f6\u95f4\u4ee3\u7801\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u65f6\u95f4\u4ee3\u7801\u5728\u591a\u4e2a\u573a\u666f\u4e2d\u90fd\u6709\u5e7f\u6cdb\u5e94\u7528\uff0c\u6bd4\u5982\u8bb0\u5f55\u4e8b\u4ef6\u53d1\u751f\u7684\u65f6\u95f4\u3001\u751f\u6210\u65f6\u95f4\u6233\u7528\u4e8e\u6570\u636e\u5e93\u3001\u8ba1\u7b97\u65f6\u95f4\u5dee\u7b49\u3002\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u65f6\u95f4\u6233\u5e38\u7528\u4e8e\u65f6\u95f4\u5e8f\u5217\u5206\u6790\uff0c\u800c\u5728Web\u5f00\u53d1\u4e2d\uff0c\u65f6\u95f4\u6233\u53ef\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u548c\u7528\u6237\u6d3b\u52a8\u8ffd\u8e2a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e2d\u7684\u65f6\u533a\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u5904\u7406\u65f6\u533a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u6765\u786e\u4fdd\u65f6\u95f4\u7684\u6b63\u786e\u6027\u3002\u901a\u8fc7<code>pytz.timezone(&#39;Asia\/Shangh<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>&#39;)<\/code>\u53ef\u4ee5\u8bbe\u5b9a\u4e3a\u4e1c\u516b\u533a\u7684\u65f6\u95f4\u3002\u5efa\u8bae\u5728\u8fdb\u884c\u65f6\u95f4\u8fd0\u7b97\u65f6\uff0c\u59cb\u7ec8\u5c06\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\uff0c\u7136\u540e\u518d\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u65f6\u533a\uff0c\u4ee5\u907f\u514d\u7531\u4e8e\u65f6\u533a\u5dee\u5f02\u5f15\u8d77\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7f16\u5199Python\u65f6\u95f4\u4ee3\u7801\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001calendar\u6a21\u5757\u7b49\u3002dateti [&hellip;]","protected":false},"author":3,"featured_media":1007402,"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\/1007381"}],"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=1007381"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007381\/revisions"}],"predecessor-version":[{"id":1007404,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007381\/revisions\/1007404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007402"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}