Kevin
Kevin

Reputation: 21

Converting String to NSDate issue

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.

Upvotes: 1

Views: 729

Answers (2)

MishieMoo
MishieMoo

Reputation: 6680

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.

Upvotes: 1

Max Seelemann
Max Seelemann

Reputation: 9364

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:.

Upvotes: 0

Related Questions