{"id":32497,"date":"2021-05-18T15:31:06","date_gmt":"2021-05-18T19:31:06","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=32497"},"modified":"2021-05-18T15:31:06","modified_gmt":"2021-05-18T19:31:06","slug":"parsing-dates-without-times","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/05\/18\/parsing-dates-without-times\/","title":{"rendered":"Parsing Dates Without Times"},"content":{"rendered":"<p><a href=\"https:\/\/developer.apple.com\/forums\/thread\/655000\">Quinn the Eskimo<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/forums\/thread\/655000\"><p>Parsing fixed-format date strings is tricky.  For an explanation as to why, see QA1480 <a href=\"https:\/\/developer.apple.com\/library\/ios\/#qa\/qa1480\/_index.html\">NSDateFormatter and Internet Dates<\/a>.  However, there&rsquo;s an extra gotcha when you try to parse fixed-format date strings that don&rsquo;t include a time.<\/p><p>[&#8230;]<\/p><p>This is failing because, internally, the date formatter maps the date string to a set of date components (<code>DateComponents<\/code>).  The year, month, and day come from the date string, but the hour, minute, and second default to 0.  In Brazil, daylight saving time starts at midnight, and thus the date components <code>year: 2018, month: 11, day: 4, hour: 0, minute: 0, second: 0<\/code> don&rsquo;t exist in the S&atilde;o Paulo time zone.  When the date formatter runs these components through the calendar, it returns <code>nil<\/code>.<\/p><p>[&#8230;]<\/p><p>The solution here is to set the <code>defaultDate<\/code> property on the date formatter to something in the middle of the day.<\/p><\/blockquote>\n<p>It&rsquo;s not documented exactly what this does, but I presume that missing date components get taken from the <code>defaultDate<\/code>. This still makes me a bit nervous, though, because what if I have an unlucky default date whose time produces a date that doesn&rsquo;t exist in that time zone? <a href=\"https:\/\/stackoverflow.com\/questions\/24088730\/why-nsdateformatter-is-returning-null-for-a-19-10-2014-in-a-brazilian-time-zone\">Apparently<\/a>, the <code>isLenient<\/code> property can help with that.<\/p>\n\n<p>See also: <a href=\"https:\/\/github.com\/macshome\/The-Wisdom-of-Quinn\">The Wisdom of Quinn<\/a>.<\/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\/05\/26\/predictable-date-formatting\/\">Predictable Date Formatting<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Quinn the Eskimo: Parsing fixed-format date strings is tricky. For an explanation as to why, see QA1480 NSDateFormatter and Internet Dates. However, there&rsquo;s an extra gotcha when you try to parse fixed-format date strings that don&rsquo;t include a time.[&#8230;]This is failing because, internally, the date formatter maps the date string to a set of date [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2021-05-18T19:31:11Z","apple_news_api_id":"e7ede4d3-80b4-4039-bc41-1cb28d17bcfc","apple_news_api_modified_at":"2021-05-18T19:31:11Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A5-3k04C0QDm8QRyyjRe8_A","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":[69,31,1837,30,1891,568,71,364],"class_list":["post-32497","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-14","tag-mac","tag-macos-11-0","tag-nsdateformatter","tag-programming","tag-time"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32497","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=32497"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32497\/revisions"}],"predecessor-version":[{"id":32498,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32497\/revisions\/32498"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=32497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=32497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=32497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}