2

I have a string that contains a .net datetime (rawData) and I am trying to parse it into a NSDate object using a NSDateFormatter. I think my format string might be wrong or something because the dateFromString: method is returning nil. Can any one please help me figure out the issue here?

NSString *rawData = @"2009-10-20T06:01:00-07:00";
//Strip the colon out of the time zone
NSRange timezone = NSMakeRange([data length] - 3, 3);
NSString *cleanData = [rawData stringByReplacingOccurrencesOfString:@":" withString:@"" options:NSCaseInsensitiveSearch range:timezone ];

//Setup a formatter and parse it into a NSDate object
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-ddThh:mm:ssZ"];
NSDate *result = [df dateFromString: cleanData]; //Result is nil
Justin Boo
  • 10,132
  • 8
  • 50
  • 71
Mark C
  • 33
  • 5
  • Apple OS use the following information to format date: http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns which say hours in 24-hour format is HH. – Black Frog Mar 17 '11 at 00:52
  • possible duplicate of [How to parse a date string into an NSDate object in iOS?](http://stackoverflow.com/questions/4999396/how-to-parse-a-date-string-into-an-nsdate-object-in-ios) – Hot Licks Jun 20 '13 at 11:27

4 Answers4

3

With iOS 6.0 and higher, representation for timezone with colon separating hour and minutes is added.(ZZZZZ or fallback z)Refer the following link Date Formats

This makes the code very simple

NSString *dateString = @"2009-10-20T06:01:00-07:00";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"%@",date);
Anupdas
  • 10,211
  • 2
  • 35
  • 60
0

Try this:

    NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    df.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SS";
    df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease];
    NSDate *result = [df dateFromString:rawData];
Zaky German
  • 14,324
  • 4
  • 25
  • 31
  • I was not able to get this one to work, I think the formatting string is causing an issue here. Thanks for the reply tho. – Mark C Mar 17 '11 at 23:08
0

Looks like .NET outputs dates as ISO 8601 strings? For parsing these I use an open source NSFormatter subclass written by Peter Hosey, which you can find here. Unfortunately it's not trivial to parse these correctly using a simple dateFormat string. See also this question.

Community
  • 1
  • 1
Marc Charbonneau
  • 40,399
  • 3
  • 75
  • 82
  • Thanks for the links. These solutions also worked nicely as well. The ISO8601DateFormatter makes for some clean code. – Mark C Mar 17 '11 at 23:13
0

I just tested this code and it's work:

NSString *rawData = @"2009-10-20 06:01:00-07:00";
//Strip the colon out of the time zone
NSRange timezone = NSMakeRange([rawData length] - 3, 3);
NSString *cleanData = [rawData stringByReplacingOccurrencesOfString:@":" withString:@"" options:NSCaseInsensitiveSearch range:timezone ];
// Get rid of the T also
cleanData = [cleanData stringByReplacingOccurrencesOfString:@"T" withString:@" "];

//Setup a formatter and parse it into a NSDate object
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ssZZZ"];
NSDate *result = [df dateFromString: cleanData]; //Result is ni
NSLog(@"Result %@", result);

Couple of notes

  1. Get rid of the T.
  2. 24-Hour format for hours is HH
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • Did you test it on the device as well? From my experience trying to parse dates into strings using NSDateFormatter without specifying a locale or time and date styles will sometimes work on the simulator but not on the device. – Zaky German Mar 17 '11 at 00:56
  • I don't have my cable with me at the moment. I will test when I reach home and let you know. – Black Frog Mar 17 '11 at 01:20
  • I have tested this in the simulator and its working good. Thank you all very much for the help. I will test this on the device tomorrow and post my results. – Mark C Mar 17 '11 at 23:13