-1

I'm trying to format this date: 2014-03-18T03:31:14.000Z

Into this one: 2014-03-18 3:31 am

I'd like split it and store it in an array.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Kim Montano
  • 2,185
  • 4
  • 26
  • 39
  • 1
    Your input date is in GMT/UTC (that's what the `Z` means, "Zulu"). But in your output date string, do you want that in you local timezone or in GMT/UTC as well? – Rob Aug 29 '15 at 03:24
  • 1
    [This answer](http://stackoverflow.com/a/30927696/981049) from the question jtbandes referred to is what you are looking for, Kim. – Michael Dautermann Aug 29 '15 at 03:24
  • and I just reopened the question because your question goes one step further (looking for a different formatted date string), plus putting it into an array. – Michael Dautermann Aug 29 '15 at 03:26
  • What do you mean by "split it and store it in an array"? Split the date? Into what? – Khanh Nguyen Aug 29 '15 at 06:24
  • Hi @Rob, thanks for looking into this. Yes you were right, I would like to convert in a local timezone, specifically Brisbane Australia. – Kim Montano Aug 29 '15 at 07:42
  • Hi @MichaelDautermann yes you were right, that's one format to solve the timestamp. If it helps you, I'm formatting date and time from Google Calendar API. There are two different time formats: 2014-12-08T01:39:34.000Z 2015-02-12T08:45:00+10:0 I'm a bit confused on what format to use. – Kim Montano Aug 29 '15 at 07:44
  • @KhanhNguyen, I would like to split it as in let splitDateTime:Array = split(wholeDate) { $0 == " "} This way I can separate the date from the time. I need to display in date UILabel and a time UILabel. I hope that answers your question. – Kim Montano Aug 29 '15 at 07:48
  • Voting to close. First 10 google result will provide lots of info. – Sulthan Aug 29 '15 at 10:20
  • By the way, when parsing the RFC 3339/ISO 8601 date string (the one with the `T` and `Z`), make sure to set the locale, as discussed in [Technical Q&A 1480](https://developer.apple.com/library/ios/qa/qa1480/_index.html). – Rob Aug 29 '15 at 12:16
  • Thanks everyone, date formatting for me is much clearer now. – Kim Montano Sep 17 '15 at 04:15

2 Answers2

2

You need to create a NSDate object and then use a NSDateFormatter to create a string for the date portion and another for the time portion. Here, I saved you the trouble (make sure the dateFormat is correct):

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let date = formatter.dateFromString("2014-03-18T03:31:14.000Z")

formatter.locale = NSLocale.currentLocale()
formatter.dateStyle = .LongStyle
formatter.timeStyle = .NoStyle
let dateString = formatter.stringFromDate(date!)

formatter.dateStyle = .NoStyle
formatter.timeStyle = .LongStyle
let timeString = formatter.stringFromDate(date!)
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Yariv Nissim
  • 13,273
  • 1
  • 38
  • 44
  • If these date and time strings are for presenting in the UI, then I think this answer's use of `.dateStyle` and `.timeStyle` is the best way to go. But (a) use `.SSS` rather than `.000`; (b) use `HH` rather than `hh`; and (c) specify `locale` of `en_US_POSIX` when parsing the RFC 3339 date string. – Rob Aug 29 '15 at 12:25
1

The following will do. Note that I used stringFromDate twice with different format strings, instead of splitting the formatted date as you wanted. Using stringFromDate twice is more independent from the format.

Also creation of NSDateFormatter is a bit expensive, it's best to reuse it.

let z = "2014-03-18T03:31:14.000Z"
let fmt = NSDateFormatter()
fmt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = fmt.dateFromString(z)!

fmt.timeZone = NSTimeZone(abbreviation: "Australia/Brisbane")
fmt.dateFormat = "yyyy-MM-dd"
let dateString = fmt.stringFromDate(date)

fmt.dateFormat = "h:mm a"
let timeString = fmt.stringFromDate(date)

print("date: \(dateString), time: \(timeString)")
Khanh Nguyen
  • 11,112
  • 10
  • 52
  • 65
  • 1
    No, do not use `YYYY`. Always use `yyyy`. Or better, use `.dateStyle` and `.timeStyle`. Also, set locale to `en_US_POSIX` in first formatter. And regarding `NSDateFormatter` being expensive, so is the changing of the `dateFormat`, so in this case the savings is modest. – Rob Aug 29 '15 at 12:20
  • Thanks Khanh and Rob! – Kim Montano Sep 17 '15 at 04:15