Anviam Blog

A Date formatter that converts between dates and their textual representations.

Date() – 

        var currentTime: Date = Date()

        Date class always provides us UTC Time. 

        Default format of date object  2020-07-13 15:09:57 +0000.

This date is UTC(Universal Time Coordinated) Prior to 1972, this time was called Greenwich Mean Time (GMT)

If you want to convert into Local Date use this  method.

extension Date {

      func toCurrentTimezone() -> Date {

          let timeZoneDifference = TimeInterval(TimeZone.current.secondsFromGMT())

          return self.addingTimeInterval(timeZoneDifference)

      }

 }

 DateFormatter() – 

 let formatter = DateFormatter()

Instances of DateFormatter create string representations of Date objects, and convert textual representations of dates and times into Date objects. For user-visible representations of dates and times, DateFormatter provides a variety of localized presets and configuration options. 

We take an example of Date Formatter – 

“YYYY-MM-dd’T’HH:mm:ss“  – 2020–13–07T21:10:00

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). 

After that, the “T” basically serves as a separator, saying “okay, we are transitioning to a time now” and the time is listed in a similar way as date, from large to small (hour:minute:second) with a colon separating each one.

Now we take some examples of DateFormatters – 

Suppose we have date string.  –

let input = “2020-13-10T21:29:00Z”  

This is  “yyyy-MM-dd’T’HH:mm:ssZ” format.

The ‘Z’ here refers to the time zone, and if the date

Time Zone – 

zzzESTThe 3 letter name of the time zone. Falls back to GMT-08:00 (hour offset) if the name is not known.
zzzzEastern Standard TimeThe expanded time zone name, falls back to GMT-08:00 (hour offset) if name is not known.
zzzzCST-04:00Time zone with abbreviation and offset
Z-0400RFC 822 GMT format. Can also match a literal Z for Zulu (UTC) time.
ZZZZZ-04:00ISO 8601 time zone format

Get Date from string with particulate format – 

   let input = “2020-13-10T21:29:00Z”  

   Let date =  getDateFromString(strDate: input, requiredDateFormat: “yyyy-MM-dd’T’HH:mm:ss”)

   func getDateFromString(strDate: String, requiredDateFormat : String) -> Date {

        let dateFormatterGet = DateFormatter()

        dateFormatterGet.timeZone = TimeZone(abbreviation: “UTC”)  

        dateFormatterGet.dateFormat = requiredDateFormat

        return dateFormatterGet.date(from: strDate)!

    }

Note:  dateFormatterGet.timeZone = TimeZone(abbreviation: “UTC”)  // It is necessary, owing to this, our date will not be impacted by timezone. If we don’t use this , DateFormatter class convert existing dateString  to locale. (Whether it is already converted.)

Get String from Date for particulate format

 func getStringFromDate(date: Date, requiredDateFormat : String) -> String {

        let dateFormatterGet = DateFormatter()

        dateFormatterGet.dateFormat = requiredDateFormat

        dateFormatterGet.timeZone = TimeZone(abbreviation: “UTC”)

        return dateFormatterGet.string(from: date)

    }

Get String from Date for particulate format with local time zone.

static func getStringFromCurrentLocaleDate(date: Date, requiredDateFormat : String) -> String {

        let dateFormatterGet = DateFormatter()

        dateFormatterGet.dateFormat = requiredDateFormat

        dateFormatterGet.locale = NSLocale.current

        return dateFormatterGet.string(from: date)

    }

Some other expended methods that convert date string from any format to  required format- 

func convertStringToDate(dateString:String, beforeformat : String, afterFormat : String) -> Date {

    let dateFormatter = DateFormatter()

    dateFormatter.timeZone = TimeZone(abbreviation: “UTC”)

    dateFormatter.dateFormat = beforeformat

    let newDate = dateFormatter.date(from: dateString)

    dateFormatter.dateFormat = afterFormat

    dateFormatter.timeZone = TimeZone.current

    let newDateString = dateFormatter.string(from: newDate!)

    dateFormatter.timeZone = TimeZone(abbreviation: “UTC”)

    let date = dateFormatter.date(from: newDateString)

    return date!

}

func convertStringToDateFormatString(dateString:String, beforeformat : String, afterFormat : String) -> String {

    let dateFormatter = DateFormatter()

    dateFormatter.timeZone = TimeZone(abbreviation: “UTC”)

    dateFormatter.dateFormat = beforeformat

    let newDate = dateFormatter.date(from: dateString)

    let newDateFormatter = DateFormatter()

    newDateFormatter.dateFormat = afterFormat

    newDateFormatter.timeZone = TimeZone(abbreviation: “UTC”)

    let newDateString = newDateFormatter.string(from: newDate!)

    return newDateString

}

Other DateFomatter class in Swift 5 –

When working with date representations in ISO 8601 format, use ISO8601DateFormatter instead.

To represent an interval between two NSDate objects, use DateIntervalFormatter instead.

To represent a quantity of time specified by an NSDateComponents object, use DateComponentsFormatter instead.

You can get more information about  ISO 8601

you can find more information at https://en.wikipedia.org/wiki/ISO_8601.

One Response

  1. Right here is the perfect web site for everyone who would like
    to find out about this topic. You understand a whole lot its
    almost hard to argue with you (not that I really will need
    to…HaHa). You certainly put a brand new spin on a subject which has been written about
    for ages. Excellent stuff, just wonderful! Bitcoin verdienen https://t.me/s/Bitcoin_verdienen

Leave a Reply

Your email address will not be published. Required fields are marked *