Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ISO 8601 to NSDate

I have a timestamp coming from server that looks like this:

2013-04-18T08:49:58.157+0000

I've tried removing the colons, I've tried all of these:

Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?

Why NSDateFormatter can not parse date from ISO 8601 format

Here is where I am at:

+ (NSDate *)dateUsingStringFromAPI:(NSString *)dateString {


    NSDateFormatter *dateFormatter;
    dateFormatter = [[NSDateFormatter alloc] init];

    //@"yyyy-MM-dd'T'HH:mm:ss'Z'" - doesn't work
    //@"yyyy-MM-dd'T'HH:mm:ssZZZ" - doesn't work
    //@"yyyy-MM-dd'T'HH:mm:sss" - doesn't work 

    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

    // NSDateFormatter does not like ISO 8601 so strip the milliseconds and timezone
    dateString = [dateString substringWithRange:NSMakeRange(0, [dateString length]-5)];

    return [dateFormatter dateFromString:dateString];
}

One of my biggest questions is, is the date format I have above really ISO 8601? All the examples I have seen from people the formats of each are slightly different. Some have ...157-0000, others don't have anything at the end.

like image 279
random Avatar asked Jul 09 '13 21:07

random


People also ask

What is the Z in ISO 8601?

Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "T0930Z". "14:45:15 UTC" would be "14:45:15Z" or "T144515Z". The Z suffix in the ISO 8601 time representation is sometimes referred to as "Zulu time" because the same letter is used to designate the Zulu time zone.

How do I read the ISO 8601 date format?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).

Is ISO 8601 valid?

Yes it is a valid ISO 8601 date.


3 Answers

This works for me:

NSString *dateString = @"2013-04-18T08:49:58.157+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
// Always use this locale when parsing fixed format date strings
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:posix];
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"date = %@", date);
like image 194
rmaddy Avatar answered Oct 21 '22 07:10

rmaddy


There is New API from Apple! NSISO8601DateFormatter

NSString *dateSTR = @"2005-06-27T21:00:00Z";
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
NSDate *date = [formatter dateFromString:dateSTR];
NSLog(@"%@", date);
like image 30
test Avatar answered Oct 21 '22 06:10

test


I also have the native API, which is way cleaner... This is the implementation I got in my DateTimeManager class:

+ (NSDate *)getDateFromISO8601:(NSString *)strDate{

    NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
    NSDate *date = [formatter dateFromString: strDate];
    return date;
}

Just copy and paste the method, it would do the trick. Enjoy it!

like image 2
Helen Wood Avatar answered Oct 21 '22 07:10

Helen Wood