开发者

Converting String to NSDate issue

I have been looking over StackOverflow and have not found开发者_Go百科 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜