Search code examples
iosobjective-cnsdatensdateformatter

Time format ios "2014-04-28T01:03:21.827753"


Any body knows about 2014-04-28T01:03:21.827753 like format, i have the methods to handle 2014-04-28T01:03:21 format but i didn't heard about like formats any one help me , that would be highly thank-full.

Here is my code:-

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.??????"];// ? what i need to use please anyone route me in right way
NSDate *date = [dateFormatter dateFromString:timeStamp];

Solution

  • This would appear to be a variation of RFC 3339/ISO 8601. See Apple's Technical Q&A QA1480 for guidance about this format (making special note of the timezone and locale).

    Note, your string doesn't specify the timezone, but it's generally assumed to be Zulu (i.e. UTC/GMT), though you should confirm this with whomever generated this time string. Also, don't forget to specify the locale for your formatter, in case the user's calendar is not in the standard Gregorian format.

    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.locale = enUSPOSIXLocale;
    formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSSSS";
    formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    NSDate *date = [formatter dateFromString:timeStamp];