Swift - iOS - Dates and times in different format Swift - iOS - Dates and times in different format ios ios

Swift - iOS - Dates and times in different format


func convertDateFormater(date: String) -> String {       let dateFormatter = NSDateFormatter()    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"    dateFormatter.timeZone = NSTimeZone(name: "UTC")    guard let date = dateFormatter.dateFromString(date) else {        assert(false, "no date from string")        return ""    }    dateFormatter.dateFormat = "yyyy MMM EEEE HH:mm"    dateFormatter.timeZone = NSTimeZone(name: "UTC")    let timeStamp = dateFormatter.stringFromDate(date)    return timeStamp}

Edit for Swift 4

func convertDateFormatter(date: String) -> String {    let dateFormatter = DateFormatter()    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"//this your string date format    dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!    dateFormatter.locale = Locale(identifier: "your_loc_id")    let convertedDate = dateFormatter.date(from: date)    guard dateFormatter.date(from: date) != nil else {        assert(false, "no date from string")        return ""    }     dateFormatter.dateFormat = "yyyy MMM HH:mm EEEE"///this is what you want to convert format    dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!    let timeStamp = dateFormatter.string(from: convertedDate!)    return timeStamp}


As already mentioned you have to use DateFormatter to format your Date objects. The easiest way to do it is creating a read-only computed property Date extension.


Read-Only Computed Properties

A computed property with a getter but no setter is known as a read-only computed property. A read-only computed property always returns a value, and can be accessed through dot syntax, but cannot be set to a different value.

Note:

You must declare computed properties—including read-only computed properties—as variable properties with the var keyword, because their value is not fixed. The let keyword is only used for constant properties, to indicate that their values cannot be changed once they are set as part of instance initialization.

You can simplify the declaration of a read-only computed property by removing the get keyword and its braces:


extension Formatter {    static let date = DateFormatter()}

extension Date {    var europeanFormattedEn_US : String {        Formatter.date.calendar = Calendar(identifier: .iso8601)        Formatter.date.locale   = Locale(identifier: "en_US_POSIX")        Formatter.date.timeZone = .current        Formatter.date.dateFormat = "dd/M/yyyy, H:mm"        return Formatter.date.string(from: self)    }}

To convert it back you can create another read-only computed property but as a string extension:

 extension String {    var date: Date? {        return Formatter.date.date(from: self)    }    func dateFormatted(with dateFormat: String = "dd/M/yyyy, H:mm", calendar: Calendar = Calendar(identifier: .iso8601), defaultDate: Date? = nil, locale: Locale = Locale(identifier: "en_US_POSIX"), timeZone: TimeZone = .current) -> Date? {        Formatter.date.calendar = calendar        Formatter.date.defaultDate = defaultDate ?? calendar.date(bySettingHour: 12, minute: 0, second: 0, of: Date())        Formatter.date.locale = locale        Formatter.date.timeZone = timeZone        Formatter.date.dateFormat = dateFormat        return Formatter.date.date(from: self)    }}

Usage:

let dateFormatted = Date().europeanFormattedEn_US         //"29/9/2018, 16:16"if let date = dateFormatted.date {    print(date.description(with:.current)) // Saturday, September 29, 2018 at 4:16:00 PM Brasilia Standard Time\n"\    date.europeanFormattedEn_US                         // "29/9/2018, 16:27"}

let dateString = "14/7/2016"if let date = dateString.toDateFormatted(with: "dd/M/yyyy") {    print(date.description(with: .current)) // Thursday, July 14, 2016 at 12:00:00 PM Brasilia Standard Time\n"}


As Zaph stated, you need to follow the documentation. Admittedly it may not be the most straightforward when compared to other class references. The short answer is, you use Date Field Symbol Table to figure out what format you want. Once you do:

let dateFormatter = NSDateFormatter()//the "M/d/yy, H:mm" is put together from the Symbol TabledateFormatter.dateFormat = "M/d/yy, H:mm"dateFormatter.stringFromDate(NSDate())

You'll also need to be able to use the table if you need to convert a date that is a string into an NSDate.

let dateAsString = "02/12/15, 16:48"let dateFormatter = NSDateFormatter()dateFormatter.dateFormat = "M/d/yyyy, H:mm"let date = dateFormatter.dateFromString(dateAsString)