views:

89

answers:

2

Hello all,

I have been looking over StackOverflow and have not found any answers yet, if I missed a post which answers this I apologize and would be grateful for the link.

I am trying to change a string "prod.Start" into a NSDate type for comparison with today's date. The following code myDate returns "1753-01-01 00:00:00 -075258"

CODE:

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setFormatterBehavior:NSDateFormatterBehavior10_4];
[format setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *myDate = [format dateFromString: prod.Start];  //prod.Start = 1/1/1753 12:00:00 AM

Any suggestions/tips would be appreciated.

Thanks

edit:

Works now : thanks for all the help. What I thought was an error was 12AM = 0 o'clock AM

I tried 12PM and output was "1753-01-01 12:00:00 -075258"

Thank you for also explaining the "-075258" = PST was curious about that.

A: 

You're using a 10.4-style format string, but the default formatter behavior is 10.0-style. Did you make sure that your formatter is using the right style? You can change it with –setFormatterBehavior: or globally with +setDefaultFormatterBehavior:.

Max Seelemann
I just added:[format setFormatterBehavior:NSDateFormatterBehavior10_4];I tried default format instead of 10_4 as well.I still get the same output, am I still missing something?Thanks.
Kevin
A: 

You're on the right track. The NSDate compare functions only work with that standard format, so that is the right output (though if my code doesn't work, check Max's solution). You now need to compare this to today's date like so...

NSDate *todaysDate = [NSDate date];
BOOL isToday = [todaysDate isEqualToDate:myDate];

That should give you what you're looking for.

MishieMoo
thanks, theres a compare that asks before/after todays date also isn't there?
Kevin