-1

I have an iPad app, using XCode 4.5, iOS 6.0 and Storyboards. I need to create a NSCompoundPredicate using a NSDate, which I am given by the user selecting from UIDatePicker. The examples I have seen show the use of NSCalendar and NSCalendarComponents, both of which use the "default" calendar.

This is the code, using one NSPredicate, which works but gets all of the records for that date and all dates in the future, which is wrong.

NSMutableArray *apptDataArray = [NSMutableArray new];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(aStartTime = %@)", [dict objectForKey:@"selectedDate"]];

I need to use a NSCompoundPredicate because I need to get all of the records for a particular date (from 1 minute after midnight to midnight of the selected date).

Is there a way to create two NSDates (begin and end of the day) for the NSCompoundPredicate from a given NSDate, in this case: [dict objectForKey:@"selectedDate"] (which is a NSDate)? (This question is NOT a duplicate of this which uses the NSCalendar code mentioned above.)

UPDATE: updated question in bold.

Community
  • 1
  • 1
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120

3 Answers3

4

While you say that you don't want to use a NSCalendar, you already are:

From the UIDatePicker Class Reference:

calendar
The calendar to use for the date picker.

@property(nonatomic, copy) NSCalendar *calendar

Discussion
The default value of this property corresponds to the user’s current calendar as configured in Settings. This is equivalent to the value returned by calling the NSCalendar class method currentCalendar. Setting calendar to nil is equivalent to setting it to its default value.

Calendars specify the details of cultural systems used for reckoning time; they identify the beginning, length, and divisions of a year.

Basically, the only way to get accurate values that you can use for a query is to use the same calendar that was used to create your NSDate. So, the short version is: Go ahead and use a calendar. In fact, use this one:

NSCalendar *cal = [NSCalendar currentCalendar];

You can't really create a meaningful NSDate object without referencing a calendar at some point. As others have pointed out, an NSDate is stored as the number of seconds since a particular date and time. Unless you feel like completely re-writing all calendar operations in order to account for things like leap years, leap seconds, etc you should use what Apple has already given us.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
1

An NSDate represents the number of seconds since a fixed epoch. Internally, the epoch used is January 1, 2001 12:00:00AM UTC+0. Most APIs across platforms use another epoch, January 1, 1970 12:00:00AM UTC+0, called the "UNIX epoch" or "time_t epoch", so there are APIs to convert to/from that epoch as well. As a result, a date is just a single number, of type NSTimeInterval (a typedef of double.)

To get the first and last moments of a day, you need to define a "day." This is done with a calendar. Most calendars have 24-hour days, but I'm sure there's one out there that doesn't.

You say you cannot use NSCalendar because you need to work in a calendar that it does not support. Can you tell us which calendar you need to use?

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • Not using a calendar; the date is selected from UIDatePicker. – SpokaneDude Feb 02 '13 at 04:05
  • 1
    @spokane-dude the UIDatePicker, by default, [uses the `[NSCalendar currentCalendar]`](http://developer.apple.com/library/ios/documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html#//apple_ref/doc/uid/TP40006806-CH3-SW10). So yes, you *do* have a calendar. – Dave DeLong Feb 02 '13 at 04:40
0

To my mind its not possible (why you don't want to use NSCalendar?)

Nik
  • 9,063
  • 7
  • 66
  • 81
  • Because one of the parameters requires a calendar which I don't have; I just have a NSDate which I need to find all of the records that belong with that date. – SpokaneDude Feb 02 '13 at 00:00