{"id":369,"date":"2025-06-03T10:43:00","date_gmt":"2025-06-05T03:22:59","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=369"},"modified":"2025-06-05T10:43:00","modified_gmt":"2025-06-05T03:22:59","slug":"%e6%95%99%e5%ad%b8-swift-%e6%97%a5%e6%9c%9f-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e6%95%99%e5%ad%b8-swift-%e6%97%a5%e6%9c%9f-%e7%af%84%e4%be%8b\/","title":{"rendered":"\u30102025 \u6700\u65b0\u7248\u3011Swift \u65e5\u671f\u8655\u7406\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p>## Swift \u65e5\u671f\u8655\u7406\u5165\u9580<\/p>\n<p>\u5728 Swift \u4e2d\uff0c\u65e5\u671f\u8655\u7406\u662f\u4e00\u9805\u975e\u5e38\u91cd\u8981\u7684\u6280\u80fd\uff0c\u7121\u8ad6\u662f\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u986f\u793a\u7576\u524d\u6642\u9593\uff0c\u9084\u662f\u5c0d\u65e5\u671f\u9032\u884c\u8a08\u7b97\u548c\u683c\u5f0f\u5316\u3002\u96a8\u8457 Swift \u8a9e\u8a00\u7684\u6301\u7e8c\u6f14\u9032\uff0c\u8b93\u6211\u5011\u4f86\u770b\u770b\u5982\u4f55\u4f7f\u7528 **2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10** \u4f86\u9032\u884c\u65e5\u671f\u8655\u7406\u3002<\/p>\n<p>### \u7372\u53d6\u7576\u524d\u65e5\u671f<\/p>\n<p>\u4ee5\u4e0b\u662f\u7372\u53d6\u7576\u524d\u65e5\u671f\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet currentDate = Date()<br \/>\n&#8220;`<\/p>\n<p>\u9019\u884c\u7a0b\u5f0f\u78bc\u6703\u5275\u5efa\u4e00\u500b `Date` \u7269\u4ef6\uff0c\u4ee3\u8868\u7576\u524d\u7684\u65e5\u671f\u548c\u6642\u9593\u3002<\/p>\n<p>### \u683c\u5f0f\u5316\u65e5\u671f<\/p>\n<p>\u4f7f\u7528 `DateFormatter` \u4f86\u683c\u5f0f\u5316\u65e5\u671f\uff0c\u4f7f\u5176\u4ee5\u53ef\u8b80\u7684\u65b9\u5f0f\u986f\u793a\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet currentDate = Date()<br \/>\nlet dateFormatter = DateFormatter()<br \/>\ndateFormatter.locale = Locale(identifier: &#8220;zh_Hant_TW&#8221;)<br \/>\ndateFormatter.dateFormat = &#8220;yyyy-MM-dd HH:mm:ss&#8221; \/\/ \u6ce8\u610f\uff1a\u683c\u5f0f\u5316\u5b57\u6bcd\u61c9\u4f7f\u7528\u5c0f\u5beb\u7684 &#8220;yyyy&#8221;<br \/>\nlet stringDate = dateFormatter.string(from: currentDate)<\/p>\n<p>print(&#8220;\u7576\u524d\u65e5\u671f\u8207\u6642\u9593\uff1a\\(stringDate)&#8221;)<br \/>\n&#8220;`<\/p>\n<p>**\u6ce8\u610f**\uff1a\u5728 `dateFormat` \u4e2d\uff0c`YYYY` \u662f\u4e00\u500b\u4e0d\u6b63\u78ba\u7684\u683c\u5f0f\uff0c\u61c9\u4f7f\u7528\u5c0f\u5beb\u7684 `yyyy`\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5982\u679c\u5728\u4f7f\u7528 `DateFormatter` \u6642\u9047\u5230\u554f\u984c\uff0c\u8acb\u78ba\u4fdd\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<p>1. \u78ba\u4fdd `dateFormat` \u4f7f\u7528\u6b63\u78ba\u7684\u683c\u5f0f\u7b26\u865f\u3002<br \/>\n2. \u6aa2\u67e5 `locale` \u662f\u5426\u6b63\u78ba\u8a2d\u7f6e\uff0c\u9019\u53ef\u80fd\u5f71\u97ff\u5230\u65e5\u671f\u683c\u5f0f\u7684\u986f\u793a\u3002<br \/>\n3. \u5982\u679c\u8f38\u51fa\u7d50\u679c\u4e0d\u7b26\u5408\u9810\u671f\uff0c\u8acb\u6aa2\u67e5\u7cfb\u7d71\u6642\u9593\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u9664\u4e86\u986f\u793a\u7576\u524d\u65e5\u671f\uff0c\u60a8\u9084\u53ef\u4ee5\u9032\u884c\u65e5\u671f\u8a08\u7b97\uff0c\u4f8b\u5982\u52a0\u6e1b\u5929\u6578\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet calendar = Calendar.current<br \/>\nif let futureDate = calendar.date(byAdding: .day, value: 5, to: currentDate) {<br \/>\n    let futureString = dateFormatter.string(from: futureDate)<br \/>\n    print(&#8220;\u4e94\u5929\u5f8c\u7684\u65e5\u671f\uff1a\\(futureString)&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u793a\u7bc4\u4e86\u5982\u4f55\u8a08\u7b97\u5f9e\u7576\u524d\u65e5\u671f\u8d77\u4e94\u5929\u5f8c\u7684\u65e5\u671f\u3002<\/p>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><br \/>\n    <img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift \u65e5\u671f\u7bc4\u4f8b\u5716\u7247\" title=\"\"><br \/>\n<\/a><br \/>\n*\u5716\u7247\u8aaa\u660e\uff1aSwift \u65e5\u671f\u8655\u7406\u7684\u793a\u7bc4\u7a0b\u5f0f\u78bc\u3002*<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>### Q1: \u5982\u4f55\u5728 Swift \u4e2d\u8f49\u63db\u4e0d\u540c\u683c\u5f0f\u7684\u65e5\u671f\uff1f<br \/>\nA1: \u60a8\u53ef\u4ee5\u4f7f\u7528 `DateFormatter` \u8a2d\u7f6e\u4e0d\u540c\u7684 `dateFormat` \u4f86\u8f49\u63db\u65e5\u671f\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5c07 &#8220;MM\/dd\/yyyy&#8221; \u8f49\u63db\u70ba &#8220;yyyy-MM-dd&#8221;\u3002<\/p>\n<p>### Q2: \u5982\u4f55\u8655\u7406\u4f86\u81ea\u7528\u6236\u7684\u65e5\u671f\u8f38\u5165\uff1f<br \/>\nA2: \u4f7f\u7528 `DateFormatter` \u89e3\u6790\u7528\u6236\u8f38\u5165\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u5f8c\u5c07\u5176\u8f49\u63db\u70ba `Date` \u7269\u4ef6\uff0c\u9019\u6a23\u53ef\u4ee5\u9032\u884c\u9032\u4e00\u6b65\u7684\u65e5\u671f\u8a08\u7b97\u6216\u6bd4\u8f03\u3002<\/p>\n<p>### Q3: \u65e5\u671f\u683c\u5f0f\u5316\u7684\u672c\u5730\u5316\u5982\u4f55\u8655\u7406\uff1f<br \/>\nA3: \u900f\u904e\u8a2d\u7f6e `DateFormatter` \u7684 `locale` \u5c6c\u6027\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u7279\u5b9a\u5730\u5340\u4f86\u986f\u793a\u65e5\u671f\u683c\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528 `zh_Hant_TW` \u4f86\u986f\u793a\u7e41\u9ad4\u4e2d\u6587\u683c\u5f0f\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>let currentDate = Date()<br \/>\nlet dataFormatter = DateFormatter()<br \/>\ndataFormatter.locale = Locale(identifier: &#8220;zh_Hant_TW&#8221;)<br \/>\ndataFormatter.dateFormat = &#8220;YYYY-MM-dd HH:mm:ss&#8221;<br \/>\nlet stringDate = dataFormatter.string(from: currentDate)<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-369","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-5X","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=369"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/369\/revisions"}],"predecessor-version":[{"id":588,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/369\/revisions\/588"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}