{"id":958770,"date":"2024-12-27T03:33:47","date_gmt":"2024-12-26T19:33:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/958770.html"},"modified":"2024-12-27T03:33:50","modified_gmt":"2024-12-26T19:33:50","slug":"python%e6%97%a5%e6%9c%9f%e5%a6%82%e4%bd%95%e7%94%a8format","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/958770.html","title":{"rendered":"python\u65e5\u671f\u5982\u4f55\u7528format"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101736\/0fb4683c-1208-47d9-a712-f31b7feb4175.webp\" alt=\"python\u65e5\u671f\u5982\u4f55\u7528format\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u65e5\u671f\u53ef\u4ee5\u901a\u8fc7format\u65b9\u6cd5\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528strftime\u51fd\u6570\u3001f-string\u683c\u5f0f\u5316\u3001format\u65b9\u6cd5\u3002strftime\u51fd\u6570\u53ef\u4ee5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u5316\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0cf-string\u683c\u5f0f\u5316\u5219\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\u5b9e\u73b0\uff0cformat\u65b9\u6cd5\u5219\u901a\u8fc7\u5360\u4f4d\u7b26\u5b9e\u73b0\u683c\u5f0f\u5316\u3002<\/strong>\u5176\u4e2d\uff0cstrftime\u51fd\u6570\u662f\u6700\u4e3a\u5e38\u7528\u548c\u7075\u6d3b\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528strftime\u51fd\u6570\u8fdb\u884c\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>strftime\u51fd\u6570\u662fPython\u4e2ddatetime\u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u5316\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5c06\u65e5\u671f\u5bf9\u8c61\u8c03\u7528\u8be5\u65b9\u6cd5\u5e76\u4f20\u5165\u683c\u5f0f\u5b57\u7b26\u4e32\u5373\u53ef\u3002\u683c\u5f0f\u5b57\u7b26\u4e32\u7531\u7279\u6b8a\u7684\u683c\u5f0f\u4ee3\u7801\u7ec4\u6210\uff0c\u6bcf\u4e2a\u683c\u5f0f\u4ee3\u7801\u4ee3\u8868\u65e5\u671f\u6216\u65f6\u95f4\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u683c\u5f0f\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>%Y<\/strong>\uff1a\u56db\u4f4d\u6570\u7684\u5e74\u4efd<\/li>\n<li><strong>%m<\/strong>\uff1a\u4e24\u4f4d\u6570\u7684\u6708\u4efd\uff0801 \u5230 12\uff09<\/li>\n<li><strong>%d<\/strong>\uff1a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0801 \u5230 31\uff09<\/li>\n<li><strong>%H<\/strong>\uff1a\u4e24\u4f4d\u6570\u7684\u5c0f\u65f6\uff0800 \u5230 23\uff09<\/li>\n<li><strong>%M<\/strong>\uff1a\u4e24\u4f4d\u6570\u7684\u5206\u949f\uff0800 \u5230 59\uff09<\/li>\n<li><strong>%S<\/strong>\uff1a\u4e24\u4f4d\u6570\u7684\u79d2\uff0800 \u5230 59\uff09<\/li>\n<\/ul>\n<p><p>\u5177\u4f53\u4f7f\u7528\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\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>now = datetime.now()<\/p>\n<h2><strong>\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(&quot;Formatted Date:&quot;, formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/code>\u5c06\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u201c\u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2\u201d\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528f-string\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>Python 3.6\u5f15\u5165\u4e86f-string\u683c\u5f0f\u5316\uff0c\u8fd9\u662f\u4e00\u79cd\u975e\u5e38\u76f4\u89c2\u548c\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u524d\u52a0\u4e0a<code>f<\/code>\u6216<code>F<\/code>\uff0c\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u5185\u90e8\u76f4\u63a5\u5d4c\u5165Python\u8868\u8fbe\u5f0f\uff0c\u5e76\u901a\u8fc7\u5927\u62ec\u53f7<code>{}<\/code>\u6765\u754c\u5b9a\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528f-string\u683c\u5f0f\u5316\u65e5\u671f\u5bf9\u8c61\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408strftime\u65b9\u6cd5\uff0c\u76f4\u63a5\u5728\u5927\u62ec\u53f7\u4e2d\u5d4c\u5165\u65e5\u671f\u5bf9\u8c61\u548c\u683c\u5f0f\u5316\u65b9\u6cd5\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>now = datetime.now()<\/p>\n<h2><strong>\u4f7f\u7528f-string\u683c\u5f0f\u5316<\/strong><\/h2>\n<p>formatted_date = f&quot;{now:%Y-%m-%d %H:%M:%S}&quot;<\/p>\n<p>print(&quot;Formatted Date using f-string:&quot;, formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>{now:%Y-%m-%d %H:%M:%S}<\/code>\u901a\u8fc7f-string\u683c\u5f0f\u5316\u76f4\u63a5\u5c06\u65e5\u671f\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528format\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u867d\u7136format\u65b9\u6cd5\u5728\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5982f-string\u76f4\u89c2\uff0c\u4f46\u5b83\u4ecd\u7136\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002format\u65b9\u6cd5\u901a\u8fc7\u5360\u4f4d\u7b26<code>{}<\/code>\u6765\u63d2\u5165\u53d8\u91cf\u6216\u8868\u8fbe\u5f0f\uff0c\u5e76\u901a\u8fc7<code>:<\/code>\u540e\u9762\u6307\u5b9a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u683c\u5f0f\u5316\u65e5\u671f\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528datetime\u5bf9\u8c61\u7684strftime\u65b9\u6cd5\u6765\u5b9e\u73b0\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>now = datetime.now()<\/p>\n<h2><strong>\u4f7f\u7528format\u65b9\u6cd5\u683c\u5f0f\u5316<\/strong><\/h2>\n<p>formatted_date = &quot;{:%Y-%m-%d %H:%M:%S}&quot;.format(now)<\/p>\n<p>print(&quot;Formatted Date using format:&quot;, formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&quot;{:%Y-%m-%d %H:%M:%S}&quot;.format(now)<\/code>\u5229\u7528format\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u65e5\u671f\u683c\u5f0f\u5316\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u5728\u65e5\u5fd7\u8bb0\u5f55\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u8bb0\u5f55\u4e8b\u4ef6\u53d1\u751f\u7684\u51c6\u786e\u65f6\u95f4\uff0c\u56e0\u6b64\u683c\u5f0f\u5316\u65e5\u671f\u5b57\u7b26\u4e32\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\u3002\u901a\u8fc7\u683c\u5f0f\u5316\u65e5\u671f\uff0c\u53ef\u4ee5\u786e\u4fdd\u65e5\u5fd7\u65f6\u95f4\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u4ef6\u540d\u547d\u540d<\/strong>\uff1a\u5728\u81ea\u52a8\u5316\u811a\u672c\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u521b\u5efa\u4ee5\u65e5\u671f\u547d\u540d\u7684\u6587\u4ef6\u3002\u901a\u8fc7\u683c\u5f0f\u5316\u5f53\u524d\u65e5\u671f\uff0c\u53ef\u4ee5\u751f\u6210\u552f\u4e00\u4e14\u6709\u610f\u4e49\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u4e2d\uff0c\u65f6\u95f4\u6233\u7684\u683c\u5f0f\u5316\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u901a\u8fc7\u5c06\u65f6\u95f4\u6233\u683c\u5f0f\u5316\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u53ef\u8bfb\u6027\u548c\u5206\u6790\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528strftime\u65b9\u6cd5\u8fdb\u884c\u65e5\u671f\u683c\u5f0f\u5316\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e0d\u540c\u5e73\u53f0\u548c\u8bed\u8a00\u73af\u5883\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u4e00\u4e9b\u5e73\u53f0\u53ef\u80fd\u4e0d\u652f\u6301\u6240\u6709\u7684\u683c\u5f0f\u4ee3\u7801\uff0c\u6216\u8005\u4e0d\u540c\u8bed\u8a00\u73af\u5883\u4e0b\u7684\u683c\u5f0f\u5316\u7ed3\u679c\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u5728\u8de8\u5e73\u53f0\u5e94\u7528\u4e2d\uff0c\u5e94\u5c3d\u91cf\u4f7f\u7528\u6807\u51c6\u7684\u683c\u5f0f\u4ee3\u7801\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u8fdb\u884c\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u5728\u5904\u7406\u65f6\u533a\u4fe1\u606f\u65f6\uff0cPython\u7684datetime\u6a21\u5757\u63d0\u4f9b\u4e86pytz\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u65f6\u533a\u8f6c\u6362\u548c\u683c\u5f0f\u5316\u3002\u5728\u9700\u8981\u5904\u7406\u65f6\u533a\u4fe1\u606f\u7684\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408pytz\u5e93\u548cstrftime\u65b9\u6cd5\u5b9e\u73b0\u51c6\u786e\u7684\u65e5\u671f\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65e5\u671f\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u5305\u62ecstrftime\u51fd\u6570\u3001f-string\u683c\u5f0f\u5316\u548cformat\u65b9\u6cd5\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u65e5\u671f\u683c\u5f0f\u5316\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u65e5\u671f\u683c\u5f0f\u5316\u4e3a\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>strftime<\/code>\u65b9\u6cd5\u6765\u5c06\u65e5\u671f\u683c\u5f0f\u5316\u4e3a\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u60f3\u8981\u7684\u683c\u5f0f\uff0c\u6bd4\u5982<code>&quot;%Y-%m-%d&quot;<\/code>\u8868\u793a\u201c\u5e74-\u6708-\u65e5\u201d\uff0c<code>&quot;%d\/%m\/%Y&quot;<\/code>\u8868\u793a\u201c\u65e5\/\u6708\/\u5e74\u201d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\nnow = datetime.now()\nformatted_date = now.strftime(&quot;%Y-%m-%d&quot;)\nprint(formatted_date)  # \u8f93\u51fa\u7c7b\u4f3c\u4e8e2023-10-05\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u7684\u65e5\u671f\u683c\u5f0f\u5316\u652f\u6301\u54ea\u4e9b\u5e38\u89c1\u7684\u683c\u5f0f\u7b26\uff1f<\/strong><br \/>\u5728Python\u7684<code>strftime<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6709\u5f88\u591a\u5e38\u7528\u7684\u683c\u5f0f\u7b26\u53ef\u4f9b\u4f7f\u7528\u3002\u6bd4\u5982\uff1a<\/p>\n<ul>\n<li><code>%Y<\/code>\uff1a\u56db\u4f4d\u6570\u7684\u5e74\u4efd\uff08\u4f8b\u5982\uff1a2023\uff09<\/li>\n<li><code>%m<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u6708\u4efd\uff0801\u523012\uff09<\/li>\n<li><code>%d<\/code>\uff1a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0801\u523031\uff09<\/li>\n<li><code>%H<\/code>\uff1a24\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6\uff0800\u523023\uff09<\/li>\n<li><code>%M<\/code>\uff1a\u5206\u949f\uff0800\u523059\uff09<\/li>\n<li><code>%S<\/code>\uff1a\u79d2\uff0800\u523059\uff09<\/li>\n<\/ul>\n<p>\u7ed3\u5408\u8fd9\u4e9b\u7b26\u53f7\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u51fa\u591a\u79cd\u5f62\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65e5\u671f\u8f6c\u6362\u4e3aPython\u7684\u65e5\u671f\u5bf9\u8c61\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>datetime.strptime<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65e5\u671f\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u3002\u4f60\u9700\u8981\u63d0\u4f9b\u5b57\u7b26\u4e32\u548c\u5bf9\u5e94\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ndate_string = &quot;2023-10-05&quot;\ndate_object = datetime.strptime(date_string, &quot;%Y-%m-%d&quot;)\nprint(date_object)  # \u8f93\u51fa\u4e3adatetime\u5bf9\u8c61\uff0c\u8868\u793a2023\u5e7410\u67085\u65e5\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u8f6c\u6362\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u4ece\u6587\u4ef6\u8bfb\u53d6\u65e5\u671f\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u65e5\u671f\u53ef\u4ee5\u901a\u8fc7format\u65b9\u6cd5\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528strftime\u51fd\u6570\u3001f-stri [&hellip;]","protected":false},"author":3,"featured_media":958776,"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\/958770"}],"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=958770"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958770\/revisions"}],"predecessor-version":[{"id":958777,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/958770\/revisions\/958777"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/958776"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=958770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=958770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=958770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}