Is there a way I can format this String for example "2014-08-22T18:30:00Z"
to 2 substrings like 2014-08-22
and 18:30
?

- 1,478
- 4
- 18
- 24
-
possible duplicate of [Swift: NSDate formatting with strftime & localtime](http://stackoverflow.com/questions/24255020/swift-nsdate-formatting-with-strftime-localtime) – David Nov 17 '14 at 14:19
-
You simply want to split the string into 2 strings? – zisoft Nov 17 '14 at 14:29
-
1possible duplicate of _thousand similar post on stackoverflow.com_... (ouch). – holex Nov 17 '14 at 14:43
1 Answers
I'd suggest that you do not want to consider these two different strings. I'd suggest the real question is how to convert this ISO 8601 / RFC 3339 date string into a NSDate
. And then you can create separate strings for the date and time if you want.
But you probably want to convert them into a single NSDate
object, because that original string is in Zulu (i.e. GMT/UTC), but you may want to show the final date and time in the local time zone. But if you return them as separate strings, when you do time zone adjustment, it becomes a hassle to correct the date when the time zone change results in a different day of the year.
Anyway, this is how to parse the "2014-08-22T18:30:00Z"
string:
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let date = formatter.dateFromString("2014-08-22T18:30:00Z")
Note, the locale
issue is a subtle one that many people overlook and discussed in Technical Q&A QA1480.

- 415,655
- 72
- 787
- 1,044
-
1sorry, was by accident. Was checking my old posts and must have clicked on it. I set it back to accepted answer – STheFox Feb 27 '15 at 20:41