-1

I am having an issue converting a string to an NSDate in Swift. My date string is in the following format:

2016-05-06T14:07:23.430Z

I have a function to return a formatted date string as follows and the date string passed into the function is in the format shown above:

private func formatDate(theDate:String?) -> String {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yy H:mm"
    let actualDate = dateFormatter.dateFromString(theDate!) //always produces nil
    let formattedDate = dateFormatter.stringFromDate(actualDate!)
    return formattedDate
}

When trying to get a date object from the string passed into the function, the date object is always nil. Can anyone assist with this issue? Thanks!

Pheepster
  • 6,045
  • 6
  • 41
  • 75
  • 2
    The date format you used (MM-dd-yy H:mm) doesn't match at all the date format of your string (2016-05-06T14:07:23.430Z). Have you read related question ? The `dateFormat` of the `NSDateFormatter` object need to match the string. – Larme May 10 '16 at 13:26
  • `dateFormatter.dateFormat` does not match the actual format of `2016-05-06T14:07:23.430Z`. You can google or search other stack overflow answers to find the available formatting options to match what you need. There is also a link in @Lion's answer (commented in code) that will point to a list of format options. – keithbhunter May 10 '16 at 13:26
  • 1
    There is a critical error in this method, you got a parameter theDate optional but where you use it in the method you use it with "!". It's wrong! Check it with a guard for example at the start of the method! – ciccioska May 10 '16 at 13:26
  • @Pheepster See my Answer working for You, – Kirit Modi May 10 '16 at 13:36
  • @Pheepster: No. The current scheme is "NSString (format 1) => NSDate => NSString (format 2)". So the [NSString (format1) + NSDateFormatter (with format1)] => NSDate => NSDateFormatter (with format2) => NSString (format2). – Larme May 10 '16 at 13:37
  • Possible duplicate of [NSDateFormatter dateFromString Always Returns nil](http://stackoverflow.com/questions/19300032/nsdateformatter-datefromstring-always-returns-nil) – Alexander May 10 '16 at 14:12

3 Answers3

3

See this answer :

let myDateString = "2016-05-06T14:07:23.430Z"

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-dd-MM'T'HH:mm:ss.SSS'Z"
let dateFromString = dateFormatter.dateFromString(myDateString)

let dateFormatter1  = NSDateFormatter()
dateFormatter1.dateFormat = "MM-dd-yy H:mm"
let getDate =  dateFormatter1.stringFromDate(dateFromString!)

print(getDate)

Your OUTPUT :

enter image description here

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
2
let strDate = "2016-05-06T14:07:23.430Z"

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let date = dateFormatter.dateFromString(strDate)
Julien Quere
  • 2,407
  • 16
  • 21
Jayesh Miruliya
  • 3,279
  • 2
  • 25
  • 22
1

You used a bad DateFormat. You should not use the same NSDateFormatter for you input date and for your output date.

Try this: yyyy-dd-MM'T'HH:mm:ss.SSS'Z' for your input. Here is a fully working solution:

func formatDate(theDate: String?) -> String {

    let inputDateFormatter = NSDateFormatter()
    inputDateFormatter.dateFormat = "yyyy-dd-MM'T'HH:mm:ss.SSS'Z'"
    let outputDateFormatter = NSDateFormatter()
    outputDateFormatter.dateFormat = "MM-dd-yy H:mm"

    let actualDate = inputDateFormatter.dateFromString(theDate!)
    let formattedDate = outputDateFormatter.stringFromDate(actualDate!)
    return formattedDate
}

formatDate("2016-05-06T14:07:23.430Z") // returns 06-05-16 14:07
Julien Quere
  • 2,407
  • 16
  • 21