Cocoa-Touch: How to See If Two Nsdates Are in the Same Day

Cocoa-Touch: How do I see if two NSDates are in the same day?

NSDateComponents sounds like the best bet to me. Another tactic to try is toll-free-bridging it to a CFDate, then using CFDateGetAbsoluteTime and doing a subtraction to get the amount of time between the two dates. You'll have to do some additional math to figure out if the time difference lands the dates on the same day, however.

Simplest way to loop between two NSDates on iPhone?

Add fast enumeration to a DateRange class:

- (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState *)state
objects: (id *)stackbuf
count: (NSUInteger)len;
{
NSInteger days = 0;
id current = nil;
id components = nil;
if (state->state == 0)
{
current = [NSCalendar currentCalendar];
state->mutationsPtr = &state->extra[0];
components = [current components: NSDayCalendarUnit
fromDate: startDate
toDate: endDate
options: 0];
days = [components day];
state->extra[0] = days;
state->extra[1] = (uintptr_t)current;
state->extra[2] = (uintptr_t)components;
} else {
days = state->extra[0];
current = (NSCalendar *)(state->extra[1]);
components = (NSDateComponents *)(state->extra[2]);
}
NSUInteger count = 0;
if (state->state <= days) {
state->itemsPtr = stackbuf;
while ( (state->state <= days) && (count < len) ) {
[components setDay: state->state];
stackbuf[count] = [current dateByAddingComponents: components
toDate: startDate
options: 0];
state->state++;
count++;
}
}
return count;
}

This is ugly, but the ugliness is confined to my date range class. My client code is just:

for (id date in dateRange) {
NSLog( @"%@ in [%@,%@]", date, startDate, endDate );
}

I think this is probably a good enough reason to create a DateRange class if you don't have one already.

How to get the difference between two dates?

Example

    NSDate *today = [NSDate date];

NSTimeInterval dateTime;


if ([visitDate isEqualToDate:today]) //visitDate is a NSDate

{

NSLog (@"Dates are equal");

}

dateTime = ([visitDate timeIntervalSinceDate:today] / 86400);

if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val

{

NSLog (@"Past Date");

}

else

{
NSLog (@"Future Date");

}

How to compare two NSDates: Which is more recent?

Let's assume two dates:

NSDate *date1;
NSDate *date2;

Then the following comparison will tell which is earlier/later/same:

if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(@"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(@"date1 is earlier than date2");
} else {
NSLog(@"dates are the same");
}

Please refer to the NSDate class documentation for more details.

Cocoa-Touch: How do I see if two NSDates are in the same day?

NSDateComponents sounds like the best bet to me. Another tactic to try is toll-free-bridging it to a CFDate, then using CFDateGetAbsoluteTime and doing a subtraction to get the amount of time between the two dates. You'll have to do some additional math to figure out if the time difference lands the dates on the same day, however.

How to determine if an NSDate is today?

In macOS 10.9+ & iOS 8+, there's a method on NSCalendar/Calendar that does exactly this!

- (BOOL)isDateInToday:(NSDate *)date 

So you'd simply do

Objective-C:

BOOL today = [[NSCalendar currentCalendar] isDateInToday:date];

Swift 3:

let today = Calendar.current.isDateInToday(date)

How to Check if an NSDate occurs between two other NSDates

I came up with a solution. If you have a better solution, feel free to leave it and I will mark it as correct.

+ (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
if ([date compare:beginDate] == NSOrderedAscending)
return NO;

if ([date compare:endDate] == NSOrderedDescending)
return NO;

return YES;
}


Related Topics



Leave a reply



Submit