Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift 3 : Convert "yyyy-MM-dd'T'HH:mm:ssZ" format string to date object

Tags:

ios

swift

nsdate

Hello i have a dictionary

self.publishedAt = dictionary["publishedAt"] as? NSString

in which i'm getting date "2017-01-27T18:36:36Z". I want to convert it in readable format : dd-MM-yyyy hh:mm:ss. i tried via

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm:ss"
let date = dateFormatter.date(from: (self.publishedAt as? String)!)
print("EXACT_DATE : \(date)") 

But getting nil. :(

What is the correct way to get date in simple format?

like image 276
Abhishek Thapliyal Avatar asked Jan 28 '17 07:01

Abhishek Thapliyal


People also ask

How do I convert a string to a date in Swift?

The class we use to convert a string to a date in Swift is DateFormatter (or NSDateFormatter if you are using Objective-C).

How do I change the date format in swift5?

I solved my problem to the format yyyy-MM-dd'T'HH:mm:ss. SSS'Z' (e.g 2018-06-15T00:00:00.000Z) with this: func formatDate(date: String) -> String { let dateFormatterGet = DateFormatter() dateFormatterGet. dateFormat = "yyyy-MM-dd'T'HH:mm:ss.

How do I format the current date in Swift?

Get current time in “YYYY-MM–DD HH:MM:SS +TIMEZONE” format in Swift. This is the easiest way to show the current date-time.

What is DateFormatter in Swift?

A formatter that converts between dates and their textual representations.


3 Answers

You need an input format to convert the ISO8601 string to date and an output format to convert the date back to string:

let string = "2017-01-27T18:36:36Z"

let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
like image 188
vadian Avatar answered Oct 21 '22 21:10

vadian


To convert string to Date object:

let string = "2017-01-27T18:36:36Z"
let isoFormatter = ISO8601DateFormatter()
let date = isoFormatter.date(from: string)!

Or, if you need to support iOS versions that predate ISO8601DateFormatter:

let isoFormatter = DateFormatter()
isoFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssX"
isoFormatter.locale = Locale(identifier: "en_US_POSIX")
isoFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let date = isoFormatter.date(from: string)!

(To understand why we set the locale for the ISO 8601 date formatter, see Apple Technical Q&A 1480.)

Then, to convert that to a user-friendly date format, you'd use a separate formatter (or use the second example, above, you can re-use the formatter, but remember to reset the locale back to Locale.current):

let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
let result = formatter.string(from:date)

Note, I'd suggest using those style parameters when presenting the date back to the user rather than the dateFormat string, so it's using the styles appropriate for their locale, rather than assuming they want 24h clock or not and/or whether they use dd-MM-yyyy vs MM-dd-yyyy format.


Note, changing the formats of date formatter (e.g. changing the dateFormat string) is a relatively expensive process, so if you're performing this process for multiple dates, do not take a single DateFormatter and constantly change its dateFormat or styles repeatedly back and forth (or worse, instantiate new formatters for each date). Instead, create one formatter per date format style and re-use it grammar as much as possible.

like image 45
Rob Avatar answered Oct 21 '22 22:10

Rob


Just used the function in your code(swift 4.2).

public func convertDateFormatter(date: String) -> String {

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"//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 = "HH:mm a"///this is what you want to convert format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
let timeStamp = dateFormatter.string(from: convertedDate!)
print(timeStamp)
return timeStamp
}

Thanks

like image 30
Nil Rathod Avatar answered Oct 21 '22 23:10

Nil Rathod