Search code examples
iphoneobjective-cnsstringnsdatenstimeinterval

Working with NSDate and Comparing with Dates in Objective C


"31-Dec-2010 9:00AM to 1:00PM"

Take the above NSString for example, I need to convert it to 2 NSDates e.g. 31-Dec-2010 9:00AM AND 31-Dec-2010 1:00PM

Then compare it with the current Date to see if the current date falls within the given dates.

So 31-Dec-2010 10:00AM would fall within.

I'm wondering What are the best practices/tricks are with Objective C to do this elegantly?


Solution

  • As it turns out, NSDateFormatter has a dateFromString method, which does exactly what you want.

    http://blog.evandavey.com/2008/12/how-to-convert-a-string-to-nsdate.html

    The official documentation for NSDateFormatter:

    http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html