{"id":208034,"date":"2025-07-08T17:02:46","date_gmt":"2025-07-08T09:02:46","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208034\/"},"modified":"2025-07-08T17:02:46","modified_gmt":"2025-07-08T09:02:46","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-time-format-%e8%8e%b7%e5%8f%96%e5%ba%8f%e6%95%b0%e6%8c%87%e7%a4%ba%e5%99%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208034\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 time.Format() \u83b7\u53d6\u5e8f\u6570\u6307\u793a\u5668\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u4f7f\u7528 time.Format() \u83b7\u53d6\u5e8f\u6570\u6307\u793a\u5668\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-28 08:39:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGolang\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u4f7f\u7528 time.Format() \u83b7\u53d6\u5e8f\u6570\u6307\u793a\u5668\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p><code>time.Format()<\/code> \u65b9\u6cd5\u4f1a\u5c06\u53c2\u6570 <code>January 2, 2006<\/code> \u89e3\u6790\u4e3a\u8f93\u51fa <code>June 20, 2021<\/code>\uff0c\u4f46\u4e0d\u4f1a\u5c06 <code>January 2nd, 2006<\/code> \u89e3\u6790\u4e3a <code>June 20th\uff0c 2021<\/code> \u5e76\u6539\u4e3a\u8f93\u51fa <code>June 20nd, 2021<\/code>.<\/p>\n<p>\u6211\u5982\u4f55\u544a\u8bc9\u5b83\u4f7f\u7528\u6b63\u786e\u7684\u5e8f\u6570\u6307\u793a\u7b26\uff1a<code>st<\/code>\/<code>nd<\/code>\/<code>rd<\/code>\/<code>th<\/code> \u540e\u7f00\uff1f\u6211\u6ca1\u6709\u770b\u5230\u6587\u6863\u4e2d\u63d0\u5230\u5b83\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6807\u51c6\u5e93\u4e2d\u4e0d\u652f\u6301\u5e8f\u6570\u6307\u793a\u7b26\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u6b64 \u7684\u5e2e\u52a9\u81ea\u884c\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u8f6f\u4ef6\u5305\u3002<\/p>\n<p>go \u6709\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5305\u53ef\u4ee5\u5c06\u8f93\u5165\u6574\u6570\u8f6c\u6362\u4e3a\u5177\u6709\u6b63\u786e\u5e8f\u6570\u6307\u793a\u7b26\u7684\u5b57\u7b26\u4e32\uff1b\u770b\u770b\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5982\u679c\u60a8\u6df1\u5165\u4e86\u89e3 go-humanize \u5bf9\u5e8f\u6570\u6307\u793a\u7b26\u7684\u652f\u6301\u7684\u5b9e\u73b0\uff1b\u60a8\u4f1a\u6ce8\u610f\u5230\u8fd9\u975e\u5e38\u7b80\u5355\uff0c\u6240\u4ee5\u6211\u66f4\u613f\u610f\u901a\u8fc7\u53c2\u8003\u6211\u94fe\u63a5\u7684\u4e24\u4e2a\u5b9e\u73b0\u6765\u81ea\u5df1\u5b9e\u73b0\u5b83\u3002\u53c2\u8003\uff1a<\/p>\n<p>\u6216\u8005\uff0c\u4f7f\u7528 go-humanize\u3002<\/p>\n<p>\u8981\u6dfb\u52a0\u53f3\u5e8f\u6570\u6307\u793a\u7b26\uff1a<code>st\/nd\/rd\/th<\/code> \u540e\u7f00\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>switch case<\/code> \u3002\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u793a\u4f8b\u7a0b\u5e8f\u6765\u6253\u5370\u6240\u6709\u65e5\u671f\u6708\u4efd\u53ca\u5176\u5404\u81ea\u7684\u540e\u7f00\u5982\u4e0b\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc getdaysuffix(n int) string {\n\n    if n &gt;= 11 &amp;&amp; n &lt;= 13 {\n        return \"th\"\n    } else {\n\n        switch n % 10 {\n        case 1:\n            return \"st\"\n        case 2:\n            return \"nd\"\n        case 3:\n            return \"rd\"\n        default:\n            return \"th\"\n        }\n    }\n}\n\nfunc main() {\n\n    for i := 1; i &lt; 32; i++ {\n        suf := getdaysuffix(i)\n        fmt.println(i, suf)\n\n    }\n\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>1 st\n2 nd\n3 rd\n4 th\n5 th\n6 th\n7 th\n8 th\n9 th\n10 th\n11 th\n12 th\n13 th\n14 th\n15 th\n16 th\n17 th\n18 th\n19 th\n20 th\n21 st\n22 nd\n23 rd\n24 th\n25 th\n26 th\n27 th\n28 th\n29 th\n30 th\n31 st<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208034","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208034"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208034\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}