{"id":1076747,"date":"2025-01-08T11:56:05","date_gmt":"2025-01-08T03:56:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076747.html"},"modified":"2025-01-08T11:56:07","modified_gmt":"2025-01-08T03:56:07","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e5%8c%96%e4%b8%ba%e6%97%a5%e6%9c%9f%e5%ad%97%e7%ac%a6%e4%b8%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076747.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u5316\u4e3a\u65e5\u671f\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\/24181149\/04370a1a-81f4-4710-b61a-080c3e9e30ba.webp\" alt=\"python\u5982\u4f55\u8f6c\u5316\u4e3a\u65e5\u671f\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65e5\u671f\u5b57\u7b26\u4e32\uff1a\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>date.isoformat()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>f-string<\/code>\u683c\u5f0f\u5316<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5<\/p>\n<p><code>strftime<\/code>\u65b9\u6cd5\u662fPython\u4e2d\u5e38\u7528\u7684\u65e5\u671f\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u7684\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\u65e5\u671f\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(date_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>strftime<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u65e5\u671f\u65f6\u95f4\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4e2d\u7684\u5404\u4e2a\u90e8\u5206\u5206\u522b\u8868\u793a\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>date.isoformat()<\/code>\u65b9\u6cd5<\/p>\n<p><code>isoformat<\/code>\u65b9\u6cd5\u662fPython\u65e5\u671f\u5bf9\u8c61\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3aISO 8601\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>isoformat<\/code>\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import date<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f<\/strong><\/h2>\n<p>today = date.today()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aISO 8601\u683c\u5f0f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = today.isoformat()<\/p>\n<p>print(date_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>isoformat<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a&quot;YYYY-MM-DD&quot;\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5<\/p>\n<p><code>format<\/code>\u65b9\u6cd5\u662fPython\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5\u7684\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\u65e5\u671f\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &quot;{:%Y-%m-%d %H:%M:%S}&quot;.format(now)<\/p>\n<p>print(date_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>format<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>f-string<\/code>\u683c\u5f0f\u5316<\/p>\n<p><code>f-string<\/code>\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u8868\u8fbe\u5f0f\u5d4c\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>f-string<\/code>\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\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\u65e5\u671f\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = f&quot;{now:%Y-%m-%d %H:%M:%S}&quot;<\/p>\n<p>print(date_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>f-string<\/code>\u901a\u8fc7\u5728\u5927\u62ec\u53f7\u5185\u5d4c\u5165\u8868\u8fbe\u5f0f\u6765\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime, date<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u4f7f\u7528strftime\u65b9\u6cd5<\/strong><\/h2>\n<p>date_str1 = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(f&quot;strftime: {date_str1}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528isoformat\u65b9\u6cd5<\/strong><\/h2>\n<p>today = date.today()<\/p>\n<p>date_str2 = today.isoformat()<\/p>\n<p>print(f&quot;isoformat: {date_str2}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528format\u65b9\u6cd5<\/strong><\/h2>\n<p>date_str3 = &quot;{:%Y-%m-%d %H:%M:%S}&quot;.format(now)<\/p>\n<p>print(f&quot;format: {date_str3}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528f-string\u683c\u5f0f\u5316<\/strong><\/h2>\n<p>date_str4 = f&quot;{now:%Y-%m-%d %H:%M:%S}&quot;<\/p>\n<p>print(f&quot;f-string: {date_str4}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>strftime<\/code>\u3001<code>isoformat<\/code>\u3001<code>format<\/code>\u65b9\u6cd5\u548c<code>f-string<\/code>\u683c\u5f0f\u5316\u65b9\u5f0f\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u8f93\u51fa\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86Python\u4e2d\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<code>strftime<\/code>\u65b9\u6cd5\u3001<code>isoformat<\/code>\u65b9\u6cd5\u3001<code>format<\/code>\u65b9\u6cd5\u548c<code>f-string<\/code>\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e0d\u540c\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u60a8\u6307\u5b9a\u65e5\u671f\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c<code>date_object.strftime(&quot;%Y-%m-%d&quot;)<\/code>\u5c06\u65e5\u671f\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u201cYYYY-MM-DD\u201d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u652f\u6301\u54ea\u4e9b\u65e5\u671f\u683c\u5f0f\u5316\u7b26\u53f7\uff1f<\/strong><br \/>Python\u7684<code>strftime()<\/code>\u65b9\u6cd5\u652f\u6301\u591a\u79cd\u683c\u5f0f\u5316\u7b26\u53f7\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>%Y<\/code>\uff1a\u56db\u4f4d\u5e74\u4efd<\/li>\n<li><code>%m<\/code>\uff1a\u4e24\u4f4d\u6708\u4efd<\/li>\n<li><code>%d<\/code>\uff1a\u4e24\u4f4d\u65e5\u671f<\/li>\n<li><code>%H<\/code>\uff1a24\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6<\/li>\n<li><code>%M<\/code>\uff1a\u5206\u949f<\/li>\n<li><code>%S<\/code>\uff1a\u79d2\u949f<br \/>\u901a\u8fc7\u7ec4\u5408\u8fd9\u4e9b\u7b26\u53f7\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0d\u5408\u6cd5\u7684\u65e5\u671f\u683c\u5f0f\u6216\u8f6c\u6362\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u65e5\u671f\u8f6c\u6362\u65f6\uff0c\u786e\u4fdd\u8f93\u5165\u7684\u65e5\u671f\u5bf9\u8c61\u662f\u6709\u6548\u7684\u3002\u53ef\u4ee5\u901a\u8fc7<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u6349\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u4f8b\u5982<code>ValueError<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u5904\u7406\u65e0\u6548\u65e5\u671f\u5bfc\u81f4\u7684\u9519\u8bef\u3002\u63d0\u4f9b\u6e05\u6670\u7684\u9519\u8bef\u63d0\u793a\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u7406\u89e3\u95ee\u9898\u6240\u5728\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65e5\u671f\u5b57\u7b26\u4e32\uff1a\u4f7f\u7528strftime\u65b9\u6cd5\u3001\u4f7f\u7528date.isof [&hellip;]","protected":false},"author":3,"featured_media":1076753,"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\/1076747"}],"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=1076747"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076747\/revisions"}],"predecessor-version":[{"id":1076756,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076747\/revisions\/1076756"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076753"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}