{"id":132,"date":"2020-08-07T05:21:30","date_gmt":"2020-08-07T05:21:30","guid":{"rendered":"http:\/\/localhost\/anviam-blog\/?p=132"},"modified":"2024-09-26T04:30:06","modified_gmt":"2024-09-26T04:30:06","slug":"date-formatter-in-swift","status":"publish","type":"post","link":"https:\/\/anviam.com\/blog\/date-formatter-in-swift\/","title":{"rendered":"Date formatter in swift"},"content":{"rendered":"\r\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<p><strong>A Date formatter that converts between dates and their textual representations.<\/strong><\/p>\r\n<\/div><\/div>\r\n<\/div><\/div>\r\n\r\n\r\n\r\n<p><strong>Date() &#8211;<\/strong>&nbsp;<\/p>\r\n\r\n\r\n\r\n<div class=\"wp-block-group alignwide\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<div class=\"wp-block-group alignwide\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<div class=\"wp-block-group alignwide\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<p class=\"has-text-align-left\">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;currentTime: Date = Date()<\/p>\r\n<\/div><\/div>\r\n<\/div><\/div>\r\n<\/div><\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-group alignwide\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<div class=\"wp-block-group alignwide\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Date class always provides us UTC Time.&nbsp;<\/p>\r\n<\/div><\/div>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Default format of date object <strong>&#8211;<\/strong>&nbsp;2020-07-13 15:09:57 +0000.<\/p>\r\n\r\n\r\n\r\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<p>        This date is UTC(Universal Time Coordinated<strong>) P<\/strong>rior to 1972, this time was called Greenwich Mean Time (GMT)<\/p>\r\n<\/div><\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<p><strong>If you want to convert into Local Date use this&nbsp;&nbsp;method.<\/strong><\/p>\r\n<\/div><\/div>\r\n<\/div><\/div>\r\n<\/div><\/div>\r\n<\/div><\/div>\r\n<\/div><\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-group alignwide\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\r\n<p>extension&nbsp;Date {<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp;&nbsp;func&nbsp;toCurrentTimezone() -&gt; Date {<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;let&nbsp;timeZoneDifference = TimeInterval(TimeZone.current.secondsFromGMT())<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;self.addingTimeInterval(timeZoneDifference)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp;&nbsp;}<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;}<\/p>\r\n<\/div><\/div>\r\n\r\n\r\n\r\n<p><\/p>\r\n<\/div><\/div>\r\n\r\n\r\n\r\n<p><strong>&nbsp;DateFormatter() &#8211;&nbsp;<\/strong><\/p>\r\n\r\n\r\n\r\n<p>&nbsp;let&nbsp;formatter = DateFormatter()<\/p>\r\n\r\n\r\n\r\n<p>Instances of&nbsp;DateFormatter&nbsp;create string representations of Date objects, and convert textual representations of dates and times into&nbsp;Date&nbsp;objects. For user-visible representations of dates and times,&nbsp;DateFormatter&nbsp;provides a variety of localized presets and configuration options.&nbsp;<\/p>\r\n\r\n\r\n\r\n<p>We take an example of Date Formatter &#8211;&nbsp;<\/p>\r\n\r\n\r\n\r\n<p>\u201cYYYY-MM-dd\u2019T\u2019HH:mm:ss\u201c&nbsp;&nbsp;&#8211; 2020\u201313\u201307T21:10:00<\/p>\r\n\r\n\r\n\r\n<p>Looking at this, it would seem that the first part is the date (13th July, 2020) and the second part is the time (21:10:00, or 9:10pm in standard time).&nbsp;<\/p>\r\n\r\n\r\n\r\n<p>After that, the \u201cT\u201d basically serves as a separator, saying \u201cokay, we are transitioning to a time now\u201d and the time is listed in a similar way as date, from large to small (hour:minute:second) with a colon separating each one.<\/p>\r\n\r\n\r\n\r\n<p>Now we take some examples of DateFormatters &#8211;&nbsp;<\/p>\r\n\r\n\r\n\r\n<p>Suppose we have date string.&nbsp;&nbsp;&#8211;<\/p>\r\n\r\n\r\n\r\n<p>let input = &#8220;2020-13-10T21:29:00Z&#8221;&nbsp;&nbsp;<\/p>\r\n\r\n\r\n\r\n<p>This is&nbsp;&nbsp;&#8220;yyyy-MM-dd&#8217;T&#8217;HH:mm:ssZ&#8221; format.<\/p>\r\n\r\n\r\n\r\n<p>The&nbsp;&#8216;Z&#8217;&nbsp;here refers to the time zone, and if the date<\/p>\r\n\r\n\r\n\r\n<p><\/p>\r\n\r\n\r\n\r\n<p><strong>Time Zone &#8211;&nbsp;<\/strong><\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-table\"><table><tbody><tr><td>zzz<\/td><td>EST<\/td><td>The 3 letter name of the time zone. Falls back to GMT-08:00 (hour offset) if the name is not known.<\/td><\/tr><tr><td>zzzz<\/td><td>Eastern Standard Time<\/td><td>The expanded time zone name, falls back to GMT-08:00 (hour offset) if name is not known.<\/td><\/tr><tr><td>zzzz<\/td><td>CST-04:00<\/td><td>Time zone with abbreviation and offset<\/td><\/tr><tr><td>Z<\/td><td>-0400<\/td><td>RFC 822 GMT format. Can also match a literal Z for Zulu (UTC) time.<\/td><\/tr><tr><td>ZZZZZ<\/td><td>-04:00<\/td><td>ISO 8601 time zone format<\/td><\/tr><\/tbody><\/table><\/figure>\r\n\r\n\r\n\r\n<p><\/p>\r\n\r\n\r\n\r\n<p><strong>Get Date from string with particulate format &#8211;&nbsp;<\/strong><\/p>\r\n\r\n\r\n\r\n<p>&nbsp;&nbsp;&nbsp;let input = &#8220;2020-13-10T21:29:00Z&#8221;&nbsp;&nbsp;<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;&nbsp;&nbsp;Let date =&nbsp;&nbsp;getDateFromString(strDate: input, requiredDateFormat: &#8220;yyyy-MM-dd&#8217;T&#8217;HH:mm:ss&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;&nbsp;&nbsp;func&nbsp;getDateFromString(strDate: String, requiredDateFormat : String) -&gt; Date {<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;let&nbsp;dateFormatterGet = DateFormatter()<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;dateFormatterGet.timeZone = TimeZone(abbreviation: &#8220;UTC&#8221;)&nbsp;&nbsp;<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;dateFormatterGet.dateFormat = requiredDateFormat<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;dateFormatterGet.date(from: strDate)!<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;}<\/p>\r\n\r\n\r\n\r\n<p><strong>Note:<\/strong>&nbsp;&nbsp;dateFormatterGet.timeZone = TimeZone(abbreviation: &#8220;UTC&#8221;)&nbsp;&nbsp;\/\/ It is necessary, owing to this, our date will not be impacted by timezone. If we don\u2019t use this , DateFormatter class convert existing dateString&nbsp;&nbsp;to locale. (Whether it is already converted.)<\/p>\r\n\r\n\r\n\r\n<p><\/p>\r\n\r\n\r\n\r\n<p><strong>Get String from Date for particulate format<\/strong><\/p>\r\n\r\n\r\n\r\n<p>&nbsp;func&nbsp;getStringFromDate(date: Date, requiredDateFormat : String) -&gt; String {<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;let&nbsp;dateFormatterGet = DateFormatter()<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;dateFormatterGet.dateFormat = requiredDateFormat<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;dateFormatterGet.timeZone = TimeZone(abbreviation: &#8220;UTC&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;dateFormatterGet.string(from: date)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;}<\/p>\r\n\r\n\r\n\r\n<p><\/p>\r\n\r\n\r\n\r\n<p><strong>Get String from Date for particulate format with local time zone.<\/strong><\/p>\r\n\r\n\r\n\r\n<p>static&nbsp;func&nbsp;getStringFromCurrentLocaleDate(date: Date, requiredDateFormat : String) -&gt; String {<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;let&nbsp;dateFormatterGet = DateFormatter()<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;dateFormatterGet.dateFormat = requiredDateFormat<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;dateFormatterGet.locale = NSLocale.current<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;dateFormatterGet.string(from: date)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;}<\/p>\r\n\r\n\r\n\r\n<p><\/p>\r\n\r\n\r\n\r\n<p><strong>Some other expended methods that convert date string from any format to&nbsp;&nbsp;required format-&nbsp;<\/strong><\/p>\r\n\r\n\r\n\r\n<p>func&nbsp;convertStringToDate(dateString:String, beforeformat : String, afterFormat : String) -&gt; Date {<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;let&nbsp;dateFormatter = DateFormatter()<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;dateFormatter.timeZone = TimeZone(abbreviation: &#8220;UTC&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;dateFormatter.dateFormat = beforeformat<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;let&nbsp;newDate = dateFormatter.date(from: dateString)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;dateFormatter.dateFormat = afterFormat<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;dateFormatter.timeZone = TimeZone.current<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;let&nbsp;newDateString = dateFormatter.string(from: newDate!)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;dateFormatter.timeZone = TimeZone(abbreviation: &#8220;UTC&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;let&nbsp;date = dateFormatter.date(from: newDateString)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;return&nbsp;date!<\/p>\r\n\r\n\r\n\r\n<p>}<\/p>\r\n\r\n\r\n\r\n<p>func&nbsp;convertStringToDateFormatString(dateString:String, beforeformat : String, afterFormat : String) -&gt; String {<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;let&nbsp;dateFormatter = DateFormatter()<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;dateFormatter.timeZone = TimeZone(abbreviation: &#8220;UTC&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;dateFormatter.dateFormat = beforeformat<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;let&nbsp;newDate = dateFormatter.date(from: dateString)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;let&nbsp;newDateFormatter = DateFormatter()<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;newDateFormatter.dateFormat = afterFormat<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;newDateFormatter.timeZone = TimeZone(abbreviation: &#8220;UTC&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;let&nbsp;newDateString = newDateFormatter.string(from: newDate!)<\/p>\r\n\r\n\r\n\r\n<p>&nbsp; &nbsp;&nbsp;return&nbsp;newDateString<\/p>\r\n\r\n\r\n\r\n<p>}<\/p>\r\n\r\n\r\n\r\n<p><\/p>\r\n\r\n\r\n\r\n<p><strong>Other DateFomatter class in Swift 5 &#8211;<\/strong><\/p>\r\n\r\n\r\n\r\n<p>When working with date representations in ISO 8601 format, use&nbsp;<a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/iso8601dateformatter\" target=\"_blank\" rel=\"noreferrer noopener\">ISO8601DateFormatter<\/a>&nbsp;instead.<\/p>\r\n\r\n\r\n\r\n<p>To represent an interval between two&nbsp;<a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nsdate\" target=\"_blank\" rel=\"noreferrer noopener\">NSDate<\/a>&nbsp;objects, use&nbsp;<a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/dateintervalformatter\" target=\"_blank\" rel=\"noreferrer noopener\">DateIntervalFormatter<\/a>&nbsp;instead.<\/p>\r\n\r\n\r\n\r\n<p>To represent a quantity of time specified by an&nbsp;<a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nsdatecomponents\" target=\"_blank\" rel=\"noreferrer noopener\">NSDateComponents<\/a>&nbsp;object, use&nbsp;<a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/datecomponentsformatter\" target=\"_blank\" rel=\"noreferrer noopener\">DateComponentsFormatter<\/a>&nbsp;instead.<\/p>\r\n\r\n\r\n\r\n<p>You can get more information about&nbsp;&nbsp;ISO 8601<\/p>\r\n\r\n\r\n\r\n<p>you can find more information at&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/ISO_8601\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/en.wikipedia.org\/wiki\/ISO_8601<\/a>.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>A Date formatter that converts between dates and their textual representations. Date() &#8211;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;currentTime: Date = Date() &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Date class always provides us UTC Time.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Default format of date object &#8211;&nbsp;2020-07-13 15:09:57 +0000. This date is UTC(Universal Time Coordinated) Prior to 1972, this time was called Greenwich [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":1959,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-anviam"],"rttpg_featured_image_url":{"full":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/digital-marketing.jpg",1024,536,false],"landscape":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/digital-marketing.jpg",1024,536,false],"portraits":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/digital-marketing.jpg",1024,536,false],"thumbnail":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/digital-marketing-150x150.jpg",150,150,true],"medium":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/digital-marketing-300x157.jpg",300,157,true],"large":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/digital-marketing.jpg",800,419,false],"1536x1536":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/digital-marketing.jpg",1024,536,false],"2048x2048":["https:\/\/anviam.com\/blog\/wp-content\/uploads\/2024\/09\/digital-marketing.jpg",1024,536,false]},"rttpg_author":{"display_name":"csharma","author_link":"https:\/\/anviam.com\/blog\/author\/csharma\/"},"rttpg_comment":158,"rttpg_category":"<a href=\"https:\/\/anviam.com\/blog\/category\/anviam\/\" rel=\"category tag\">Anviam<\/a>","rttpg_excerpt":"A Date formatter that converts between dates and their textual representations. Date() &#8211;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;currentTime: Date = Date() &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Date class always provides us UTC Time.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Default format of date object &#8211;&nbsp;2020-07-13 15:09:57 +0000. This date is UTC(Universal Time Coordinated) Prior to 1972, this time was called Greenwich&hellip;","_links":{"self":[{"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/posts\/132"}],"collection":[{"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":1,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":2083,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/posts\/132\/revisions\/2083"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/media\/1959"}],"wp:attachment":[{"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anviam.com\/blog\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}