Here's a way to convert an NSDate
to an NSTimeInterval
that represents midnight of the original date without using NSCalendar
. Doing this with two NSDate
objects would let you compare the two dates without regard to time.
NSDate *now = [NSDate date]; // Your original date with time
NSTimeInterval interval = [now timeIntervalSince1970]; // the full interval
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; // Zulu time
[form setDateFormat:@"A"]; // milliseconds since midnight
NSString *secondsStr = [form stringFromDate:now];
NSTimeInterval seconds = [secondsStr integerValue] / 1000.0; // seconds since midnight
NSTimeInterval justDate = interval - seconds; // interval for date at midnight (Zulu time)
// For testing purposes
NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:justDate];
NSLog(@"now = %@, interval = %f", now, interval);
NSLog(@"seconds = %@", secondsStr);
NSLog(@"justDate = %f, nowDate = %@", justDate, nowDate);
This may or may not be better than using NSCalendar
as shown in Noah's answer.
You must definitely not simply divide by SECONDS_PER_DAY
. That will simply be wrong.