Comparing Nsdate

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.

Objective C how to compare two date_time with the ignorance on second value

Nowadays, probably the best way is to use -[NSCalendar isDate:equalToDate:toUnitGranularity:].

BOOL datesAreEqual = [[NSCalendar currentCalendar] isDate:date1
equalToDate:date2 toUnitGranularity:NSCalendarUnitMinute];

To compare them (find out which one is more recent/late), use -[NSCalendar compareDate:toDate:toUnitGranularity:] instead.

Comparing two NSDates and ignoring the time component

You set the time in the date to 00:00:00 before doing the comparison:

unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar* calendar = [NSCalendar currentCalendar];

NSDateComponents* components = [calendar components:flags fromDate:date];

NSDate* dateOnly = [calendar dateFromComponents:components];

// ... necessary cleanup

Then you can compare the date values. See the overview in reference documentation.

NSDate Comparison using Swift

I like using extensions to make code more readable. Here are a few NSDate extensions that can help clean your code up and make it easy to understand. I put this in a sharedCode.swift file:

extension NSDate {

func isGreaterThanDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isGreater = false

//Compare Values
if self.compare(dateToCompare as Date) == ComparisonResult.orderedDescending {
isGreater = true
}

//Return Result
return isGreater
}

func isLessThanDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isLess = false

//Compare Values
if self.compare(dateToCompare as Date) == ComparisonResult.orderedAscending {
isLess = true
}

//Return Result
return isLess
}

func equalToDate(dateToCompare: NSDate) -> Bool {
//Declare Variables
var isEqualTo = false

//Compare Values
if self.compare(dateToCompare as Date) == ComparisonResult.orderedSame {
isEqualTo = true
}

//Return Result
return isEqualTo
}

func addDays(daysToAdd: Int) -> NSDate {
let secondsInDays: TimeInterval = Double(daysToAdd) * 60 * 60 * 24
let dateWithDaysAdded: NSDate = self.addingTimeInterval(secondsInDays)

//Return Result
return dateWithDaysAdded
}

func addHours(hoursToAdd: Int) -> NSDate {
let secondsInHours: TimeInterval = Double(hoursToAdd) * 60 * 60
let dateWithHoursAdded: NSDate = self.addingTimeInterval(secondsInHours)

//Return Result
return dateWithHoursAdded
}
}

Now if you can do something like this:

//Get Current Date/Time
var currentDateTime = NSDate()

//Get Reminder Date (which is Due date minus 7 days lets say)
var reminderDate = dueDate.addDays(-7)

//Check if reminderDate is Greater than Right now
if(reminderDate.isGreaterThanDate(currentDateTime)) {
//Do Something...
}

iOS - Comparing NSDate with another NSDate

I have found another very nice approach to do the same...
here is the code, i thought to share it with stackoverflow.
Cocoa has couple of methods for this:

in NSDate

– isEqualToDate:  
– earlierDate:
– laterDate:
– compare:

When you use - (NSComparisonResult)compare:(NSDate *)anotherDate ,you get back one of these:

The receiver and anotherDate are exactly equal to each other, NSOrderedSame
The receiver is later in time than anotherDate, NSOrderedDescending
The receiver is earlier in time than anotherDate, NSOrderedAscending.

example:

NSDate * now = [NSDate date];
NSDate * mile = [[NSDate alloc] initWithString:@"2001-03-24 10:45:32 +0600"];
NSComparisonResult result = [now compare:mile];

NSLog(@"%@", now);
NSLog(@"%@", mile);

switch (result)
{
case NSOrderedAscending: NSLog(@"%@ is in future from %@", mile, now); break;
case NSOrderedDescending: NSLog(@"%@ is in past from %@", mile, now); break;
case NSOrderedSame: NSLog(@"%@ is the same as %@", mile, now); break;
default: NSLog(@"erorr dates %@, %@", mile, now); break;
}

[mile release];

How to compare two NSDate objects in objective C

Here's an example using the NSDate method, compare:

if([startDate compare: endDate] == NSOrderedDescending) // if start is later in time than end
{
// do something
}

From the class reference:

If...

The receiver and anotherDate are exactly equal to each other,
NSOrderedSame.

The receiver is later in time than anotherDate, NSOrderedDescending.

The receiver is earlier in time than anotherDate, NSOrderedAscending.

You could also use the timeIntervalSinceDate: method if you wanted to compare two dates and find the seconds between them, for example.

EDIT:

if(([startDate1 compare:[self getDefaultDate]] == NSOrderedSame) || ([startDate1 compare: [self getDefaultDate]] != NSOrderedSame && (([m_selectedDate compare: m_currentDate1] == NSOrderedDescending) || [m_selectedDate compare: m_currentDate1] == NSOrderedSame) && cycleStopped))

NSDate, comparing two dates

Can you try below lines of code?

switch ([dateOne compare:dateTwo]){
case NSOrderedAscending:
NSLog(@"NSOrderedAscending");
break;
case NSOrderedSame:
NSLog(@"NSOrderedSame");
break;
case NSOrderedDescending:
NSLog(@"NSOrderedDescending");
break;
}

But in your case I think you need to keep both dates in same format ..or some thing like this

 NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
[components setHour:23];//change your time to 24hrs formate
[components setMinute:05];
[components setSecond:00];

NSDate *dateOne = [[NSCalendar currentCalendar] dateFromComponents:components];

components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];

NSDate *dateTwo = [[NSCalendar currentCalendar] dateFromComponents:components];

and do comparison between dateOne and dateTwo.

Compare two NSDates for same date/time

You're comparing two pointer values. You need to use the NSDate comparison method like:

return ([date1 compare:date2] == NSOrderedSame);

Comparing two NSDates date and time ignoring seconds

This seems to be working solution as of now. If anyone gets better one do post it.

-(BOOL)checkIfTimePassed{

NSDate *today = [NSDate date];

NSComparisonResult result = [[NSCalendar currentCalendar] compareDate:today toDate:self.whenDate toUnitGranularity:NSCalendarUnitMinute];

if(result==NSOrderedAscending){
NSLog(@"today is less");
return NO;
}
else if(result==NSOrderedDescending)
{
NSLog(@"newDate is less");
return YES;
}
else
{
NSLog(@"Both dates are same");
return NO;
}
}


Related Topics



Leave a reply



Submit