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.
Asked
Active
Viewed 69 times
-4

Bhavin Bhadani
- 22,224
- 10
- 78
- 108

samad5353
- 381
- 1
- 5
- 18
-
1`yyyy-MM-dd'T'HH:mm:ss.SSS` is the format here – Bhavin Bhadani Oct 19 '16 at 08:17
-
Most of the date formats are standard formats. you can refer them here https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html – rudedude Oct 19 '16 at 08:20
-
It's a UTC date format. There's many answers here. Here's one http://stackoverflow.com/questions/29392874/converting-utc-date-format-to-local-nsdate – Warren Burton Oct 19 '16 at 08:26
-
@EICaptainv2.0 Thanks !!! It worked – samad5353 Oct 19 '16 at 09:10
1 Answers
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)!
}

Mohammad Arifuzzaman
- 868
- 9
- 20
-
Your date format does not match the string from the question. And btw, that's Objective-C, not Swift. – Martin R Oct 19 '16 at 08:34
-