{"id":1073595,"date":"2025-01-08T11:27:05","date_gmt":"2025-01-08T03:27:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073595.html"},"modified":"2025-01-08T11:27:07","modified_gmt":"2025-01-08T03:27:07","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%bd%93%e5%89%8d%e6%97%b6%e9%97%b4%e5%ad%97%e7%ac%a6%e4%b8%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073595.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103447\/78a40be9-f3f5-44f8-8111-22c6e169ca3e.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528arrow\u5e93\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528datetime\u6a21\u5757<\/strong>\u662f\u4e00\u79cd\u5e38\u7528\u4e14\u7b80\u6d01\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7datetime\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528datetime\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165datetime\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528datetime.datetime.now()\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528strftime()\u65b9\u6cd5\u5c06datetime\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002strftime()\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u65f6\u95f4\u683c\u5f0f\uff0c\u4ece\u800c\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32\u4e3a:&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/code>\u6307\u5b9a\u4e86\u65f6\u95f4\u683c\u5f0f\uff1a\u5e74\u4efd\uff08\u56db\u4f4d\u6570\uff09\u3001\u6708\u4efd\u3001\u65e5\u671f\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528datetime\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>Python\u7684datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime.datetime.now()\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4:&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>datetime\u6a21\u5757\u7684strftime()\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5c06datetime\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6307\u5b9a\u65f6\u95f4\u683c\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32:&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u683c\u5f0f\u5316\u4ee3\u7801\u6765\u751f\u6210\u5404\u79cd\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>%Y<\/code>\uff1a\u56db\u4f4d\u6570\u7684\u5e74\u4efd<\/li>\n<li><code>%m<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u6708\u4efd\uff0801-12\uff09<\/li>\n<li><code>%d<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0801-31\uff09<\/li>\n<li><code>%H<\/code>\uff1a24\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6\uff0800-23\uff09<\/li>\n<li><code>%M<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u5206\u949f\uff0800-59\uff09<\/li>\n<li><code>%S<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u79d2\uff0800-59\uff09<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5e74-\u6708-\u65e5<\/p>\n<p>date_str = current_time.strftime(&quot;%Y-%m-%d&quot;)<\/p>\n<p>print(&quot;\u5e74-\u6708-\u65e5:&quot;, date_str)<\/p>\n<h2><strong>\u6708\/\u65e5\/\u5e74<\/strong><\/h2>\n<p>date_str = current_time.strftime(&quot;%m\/%d\/%Y&quot;)<\/p>\n<p>print(&quot;\u6708\/\u65e5\/\u5e74:&quot;, date_str)<\/p>\n<h2><strong>12\u5c0f\u65f6\u5236\u65f6\u95f4<\/strong><\/h2>\n<p>time_str = current_time.strftime(&quot;%I:%M:%S %p&quot;)<\/p>\n<p>print(&quot;12\u5c0f\u65f6\u5236\u65f6\u95f4:&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528time\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>Python\u7684time\u6a21\u5757\u63d0\u4f9b\u4e86\u4f4e\u7ea7\u522b\u7684\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528time.localtime()\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u7684\u672c\u5730\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = time.localtime()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4:&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>time\u6a21\u5757\u7684strftime()\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5c06\u65f6\u95f4\u5143\u7ec4\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0edatetime\u6a21\u5757\u76f8\u540c\u7684\u683c\u5f0f\u5316\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = time.localtime()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, current_time)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32:&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u683c\u5f0f\u5316\u4ee3\u7801\u6765\u751f\u6210\u5404\u79cd\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>%Y<\/code>\uff1a\u56db\u4f4d\u6570\u7684\u5e74\u4efd<\/li>\n<li><code>%m<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u6708\u4efd\uff0801-12\uff09<\/li>\n<li><code>%d<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0801-31\uff09<\/li>\n<li><code>%H<\/code>\uff1a24\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6\uff0800-23\uff09<\/li>\n<li><code>%M<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u5206\u949f\uff0800-59\uff09<\/li>\n<li><code>%S<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u79d2\uff0800-59\uff09<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5e74-\u6708-\u65e5<\/p>\n<p>date_str = time.strftime(&quot;%Y-%m-%d&quot;, current_time)<\/p>\n<p>print(&quot;\u5e74-\u6708-\u65e5:&quot;, date_str)<\/p>\n<h2><strong>\u6708\/\u65e5\/\u5e74<\/strong><\/h2>\n<p>date_str = time.strftime(&quot;%m\/%d\/%Y&quot;, current_time)<\/p>\n<p>print(&quot;\u6708\/\u65e5\/\u5e74:&quot;, date_str)<\/p>\n<h2><strong>12\u5c0f\u65f6\u5236\u65f6\u95f4<\/strong><\/h2>\n<p>time_str = time.strftime(&quot;%I:%M:%S %p&quot;, current_time)<\/p>\n<p>print(&quot;12\u5c0f\u65f6\u5236\u65f6\u95f4:&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528arrow\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5arrow\u5e93<\/h4>\n<\/p>\n<p><p>arrow\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u53cb\u597d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5arrow\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install arrow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>arrow\u5e93\u63d0\u4f9b\u4e86arrow.now()\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = arrow.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4:&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>arrow\u5e93\u7684format()\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5c06Arrow\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0edatetime\u6a21\u5757\u76f8\u540c\u7684\u683c\u5f0f\u5316\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = arrow.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = current_time.format(&quot;YYYY-MM-DD HH:mm:ss&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32:&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>arrow\u5e93\u7684\u683c\u5f0f\u5316\u4ee3\u7801\u4e0edatetime\u6a21\u5757\u7684strftime()\u65b9\u6cd5\u517c\u5bb9\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5e74-\u6708-\u65e5<\/p>\n<p>date_str = current_time.format(&quot;YYYY-MM-DD&quot;)<\/p>\n<p>print(&quot;\u5e74-\u6708-\u65e5:&quot;, date_str)<\/p>\n<h2><strong>\u6708\/\u65e5\/\u5e74<\/strong><\/h2>\n<p>date_str = current_time.format(&quot;MM\/DD\/YYYY&quot;)<\/p>\n<p>print(&quot;\u6708\/\u65e5\/\u5e74:&quot;, date_str)<\/p>\n<h2><strong>12\u5c0f\u65f6\u5236\u65f6\u95f4<\/strong><\/h2>\n<p>time_str = current_time.format(&quot;hh:mm:ss A&quot;)<\/p>\n<p>print(&quot;12\u5c0f\u65f6\u5236\u65f6\u95f4:&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528pytz\u5e93\u5904\u7406\u65f6\u533a<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5pytz\u5e93<\/h4>\n<\/p>\n<p><p>pytz\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u533a\u5904\u7406\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5pytz\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pytz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>pytz\u5e93\u5141\u8bb8\u6211\u4eec\u83b7\u53d6\u7279\u5b9a\u65f6\u533a\u7684\u5f53\u524d\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now(pytz.utc)<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4 (UTC):&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u7684strftime()\u65b9\u6cd5\u5c06datetime\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now(pytz.utc)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S %Z%z&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32:&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06datetime\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u65f6\u533a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4 (UTC)<\/strong><\/h2>\n<p>current_time = datetime.now(pytz.utc)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5317\u4eac\u65f6\u95f4<\/strong><\/h2>\n<p>beijing_tz = pytz.timezone(&quot;Asia\/Shangh<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>&quot;)<\/p>\n<p>beijing_time = current_time.astimezone(beijing_tz)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = beijing_time.strftime(&quot;%Y-%m-%d %H:%M:%S %Z%z&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32 (\u5317\u4eac):&quot;, time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u5404\u79cd\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65f6\u95f4\u683c\u5f0f\u8f6c\u6362\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. ISO 8601\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>ISO 8601\u662f\u4e00\u79cd\u56fd\u9645\u6807\u51c6\u7684\u65e5\u671f\u548c\u65f6\u95f4\u8868\u793a\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u7684isoformat()\u65b9\u6cd5\u751f\u6210ISO 8601\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<h2><strong>\u751f\u6210ISO 8601\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>iso_time_str = current_time.isoformat()<\/p>\n<p>print(&quot;ISO 8601\u683c\u5f0f\u65f6\u95f4\u5b57\u7b26\u4e32:&quot;, iso_time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u81ea\u5b9a\u4e49\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u7684strftime()\u65b9\u6cd5\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = datetime.now()<\/p>\n<h2><strong>\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>custom_time_str = current_time.strftime(&quot;%A, %B %d, %Y %I:%M %p&quot;)<\/p>\n<p>print(&quot;\u81ea\u5b9a\u4e49\u683c\u5f0f\u65f6\u95f4\u5b57\u7b26\u4e32:&quot;, custom_time_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>%A<\/code>\u8868\u793a\u661f\u671f\u51e0\u7684\u5168\u540d\uff0c<code>%B<\/code>\u8868\u793a\u6708\u4efd\u7684\u5168\u540d\uff0c<code>%d<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0c<code>%Y<\/code>\u8868\u793a\u56db\u4f4d\u6570\u7684\u5e74\u4efd\uff0c<code>%I<\/code>\u8868\u793a12\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6\uff0c<code>%M<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u5206\u949f\uff0c<code>%p<\/code>\u8868\u793aAM\u6216PM\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u89e3\u6790\u65f6\u95f4\u5b57\u7b26\u4e32\u5e76\u5c06\u5176\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u89e3\u6790\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528datetime\u6a21\u5757\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u7684strptime()\u65b9\u6cd5\u89e3\u6790\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = &quot;2023-10-01 15:30:00&quot;<\/p>\n<h2><strong>\u89e3\u6790\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>parsed_time = datetime.strptime(time_str, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u89e3\u6790\u540e\u7684\u65f6\u95f4\u5bf9\u8c61:&quot;, parsed_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528dateutil\u5e93\u89e3\u6790<\/h4>\n<\/p>\n<p><p>dateutil\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7075\u6d3b\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u89e3\u6790\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5dateutil\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-dateutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528dateutil.parser.parse()\u65b9\u6cd5\u89e3\u6790\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dateutil import parser<\/p>\n<h2><strong>\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>time_str = &quot;2023-10-01 15:30:00&quot;<\/p>\n<h2><strong>\u89e3\u6790\u65f6\u95f4\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>parsed_time = parser.parse(time_str)<\/p>\n<p>print(&quot;\u89e3\u6790\u540e\u7684\u65f6\u95f4\u5bf9\u8c61:&quot;, parsed_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>dateutil\u5e93\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u591a\u79cd\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u9700\u8981\u6211\u4eec\u624b\u52a8\u6307\u5b9a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86Python\u4e2d\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001arrow\u5e93\u548cpytz\u5e93\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u5904\u7406\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u683c\u5f0f\u4ee5\u53ca\u5982\u4f55\u89e3\u6790\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u751f\u6210\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u5e76\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u6280\u80fd\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u65f6\u95f4\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u683c\u5f0f\u5316\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>datetime<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>current_time = datetime.now().strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/code>\u5c06\u8fd4\u56de\u7c7b\u4f3c\u4e8e\u201c2023-10-10 15:30:45\u201d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u83b7\u53d6UTC\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u5f53\u524d\u7684UTC\u65f6\u95f4\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>datetime.utcnow()<\/code>\u65b9\u6cd5\u3002\u4e0e\u672c\u5730\u65f6\u95f4\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u901a\u8fc7<code>strftime()<\/code>\u65b9\u6cd5\u5bf9UTC\u65f6\u95f4\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c<code>utc_time = datetime.utcnow().strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/code>\u5c06\u8fd4\u56de\u5f53\u524dUTC\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u683c\u5f0f\u4e0e\u672c\u5730\u65f6\u95f4\u4e00\u81f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u65f6\u95f4\u6233\u5e76\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u65f6\u95f4\u6233\u662f\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7ecf\u8fc7\u7684\u79d2\u6570\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>time<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u3002\u4f7f\u7528<code>time.time()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u5148\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u518d\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\nfrom datetime import datetime\n\ntimestamp = time.time()\ntime_string = datetime.fromtimestamp(timestamp).strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u5f53\u524d\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528datetime\u6a21\u5757\u3001\u4f7f\u7528time\u6a21\u5757\u3001\u4f7f\u7528arrow\u5e93\u3002 \u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1073605,"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\/1073595"}],"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=1073595"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073595\/revisions"}],"predecessor-version":[{"id":1073608,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073595\/revisions\/1073608"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073605"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}