-4

I am getting some strange date format as string from server
2016-10-03T17:28:34.773
Which format I need to use to convert this string to date.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
samad5353
  • 381
  • 1
  • 5
  • 18

1 Answers1

1

You can use this function to get date from your string.

 - (NSDate *) dateFromServerAttributeDateFormat:(NSString *)dateString
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
        [dateFormatter setLocale:[NSLocale currentLocale]];
        if (!dateString.length){
          return nil;
          }
        else
            return [dateFormatter dateFromString:dateString];

    }

in swift

   func dateFromServerString(dateString:String) -> NSDate {
    let dateFormater = NSDateFormatter()
    dateFormater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
    dateFormater.locale = NSLocale.currentLocale()
    return dateFormater.dateFromString(dateString)!
}