Anviam

Anviam is your one-stop solution to a variety of tech services and consultation custom- suited for your business needs. From building robust applications to understanding scalability in the future, integrating efficient systems and developing modern solutions, you'll find it all with us! We're here to break down scalability for you, guide you through large-scale web application development, and equip you with practical solutions ...First is software, design decisions and IT infrastructure. The second is the scalability of teams and processes. It is difficult to build ... Developing a robust, scalable, and efficient system can be daunting . However, understanding the key concepts and components can make the ...

美國威而鋼VIAGRA

性功能障礙是男性和女性中常見的問題。它可以由生理問題和醫學狀況引起

威而鋼的效果可能因個體差異而有所不同。大樹藥局威而鋼。 預防藥:用於預防特定疾病,如疫苗或抗瘧疾藥物。

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.

Share This