{"id":34815,"date":"2022-01-26T15:52:53","date_gmt":"2022-01-26T20:52:53","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=34815"},"modified":"2025-10-29T09:38:58","modified_gmt":"2025-10-29T13:38:58","slug":"swift-foundation-formatter-improvements","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/01\/26\/swift-foundation-formatter-improvements\/","title":{"rendered":"Swift Foundation Formatter Improvements"},"content":{"rendered":"<p><a href=\"https:\/\/useyourloaf.com\/blog\/swift-foundation-formatter-improvements\/\">Keith Harrison<\/a>:<\/p>\n<blockquote cite=\"https:\/\/useyourloaf.com\/blog\/swift-foundation-formatter-improvements\/\"><p>Unfortunately the documentation is not great so here&rsquo;s a summary starting with date formatting.<\/p><p>[&#8230;]<\/p><p>In iOS 15 you apply the formatting directly to the Date without the need to create (and cache) a formatter. For example, the <code>formatted(date:time)<\/code> method applies predefined date and time styles[&#8230;]<\/p><p>[&#8230;]<\/p><p>You customize the output by adding fields to the style. The output then contains just the fields you want:<\/p><pre>now.formatted(.dateTime.year().day().month()) \/\/ Jan 23, 2022<\/pre>\n<p>[&#8230;]<\/p>\n<p>If you add the <code>.attributed<\/code> field to a format you get back a formatted attribute string. This is handy when you want to format components of the output[&#8230;]<\/p><\/blockquote>\n<p>The above example is for localized formatting, so the order that you specify the fields in doesn&rsquo;t matter, and the styles and spellings are subject to the user&rsquo;s settings.<\/p>\n<p>To specify exactly the format that you want, there&rsquo;s a new type-safe alternative to format strings. Instead of:<\/p>\n<pre>let dateFormatter = DateFormatter()\ndateFormatter.dateFormat = \"yyyy-MM-dd\"\ndateFormatter.string(from: date)<\/pre>\n<p>You can write:<\/p>\n<pre>date.formatted(Date.VerbatimFormatStyle(format: \"\\(year: .defaultDigits)-\\(month: .twoDigits)-\\(day: .twoDigits)\", \n                                        timeZone: .current, \n                                        calendar: .current))<\/pre>\n<p>Yes, unfortunately this method leads to long code lines. And you probably still need to use <code>Locale(identifier: \"en_US_POSIX\")<\/code> to get dependable results.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/04\/14\/big-sur-has-changed-how-macos-formats-time\/\">Big Sur Has Changed How macOS Formats Time<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/10\/27\/unicode-date-formats-yyyy\/\">Unicode Date Formats, YYYY?!<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2013\/01\/03\/do-not-disturb-bug\/\">Do Not Disturb Bug<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-foundation-formatter-improvements-update-2022-05-31\">Update (2022-05-31): See also: <a href=\"https:\/\/www.donnywals.com\/formatting-dates-in-swift-using-date-formatstyle-on-ios-15\/\">Donny Wals<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Keith Harrison: Unfortunately the documentation is not great so here&rsquo;s a summary starting with date formatting.[&#8230;]In iOS 15 you apply the formatting directly to the Date without the need to create (and cache) a formatter. For example, the formatted(date:time) method applies predefined date and time styles[&#8230;][&#8230;]You customize the output by adding fields to the style. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-01-26T20:52:56Z","apple_news_api_id":"e5cc2ade-c76e-4850-8e30-7db5301f1213","apple_news_api_modified_at":"2025-10-29T13:39:01Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/A5cwq3sduSFCOMH21MB8SEw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[31,2078,30,2077,568,71,2854,901,364],"class_list":["post-34815","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-15","tag-mac","tag-macos-12","tag-nsdateformatter","tag-programming","tag-swift-foundation","tag-swift-programming-language","tag-time"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=34815"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34815\/revisions"}],"predecessor-version":[{"id":36030,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34815\/revisions\/36030"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=34815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=34815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=34815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}