views:

40

answers:

2

Getting a NSString as follows:

NSString *lastSyncDate = [[items objectAtIndex:0] objectForKey:@"LastSyncDate"];

This value is: 1/1/2010 12:00:00 AM

I am using the following to convert to a date:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterFullStyle];
[df setDateFormat:@"MMMM d, y HH:mm:ss ZZZ"];
NSDate *mySyncDate = [df dateFromString:lastSyncDate];

This gives me a null value?

+4  A: 

Your date format doesn't match the format of the string.

Try

[df setDateFormat:@"M/d/y hh:mm:ss a"];

or

[df setDateFormat:@"M/d/y h:mm:ss a"]; // if the hours aren't zero-padded

The format strings are a Unicode standard described at http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Seamus Campbell
awesome, LOL - I was looking for a list somewhere that would tell me what I could use for DateFormat's I should have just guessed!
Slee
Of course, the date string depends on your region format/time zone/calendar. It's more robust to force ISO 8601-style dates ([NSDate description] is a reasonable approximation, I think), or to just store timeIntervalSinceReferenceDate or timeIntervalSince1970.
tc.
A: 

I agree with Seamus Campbell. However, be aware that setting the time style on the formatter (NSDateFormatter) will have no effect on the result returned by dateFromString.

johnnieb