ios:How to compare 2 NSdates with Different formats? - iphone

i faced a lot of problem while comparing two NSDate with different format though i am new to this.
-what should be the best approach to this problem?
1.I tried to convert this two dates in into one format and then go for comparison
or Should i first Know the format of this two date then go for comparison

NSDate does not know any thing about it's format. Internally the date is stored without any format or region.
To compare date you can use the compare: method.
if([someDate compare:ortherDate] == NSOrderedDescending ){
// someDate later then otherDate;
}

What do you mean with format? After you initialized the NSDate object, you can directly comapre it to any other object of that type like that: (assuming that date1 is of type NSDate)
if ([date1 compare:[NSDate date]] == NSOrderedAscending) {
...
}
You can see the ref here (look for the compare method): https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsdate_Class/Reference/Reference.html

Related

In swift, using timeIntervalSinceReferenceDate, NSDate is not identical to NSTimeInterval error

I've two NSDates, one stamped by the devise, and the other entered by the user. I'd like to see if there is a difference between the two, and researched the following code, but I get an error message that NSDate is not identical to NSTimeInterval.
let compareResult = recordDateTimeEntered!.compare(recordDateTimeEntered!)
let timeFromEnteredtoStampedDates = recordDateTimeStamped.timeIntervalSinceReferenceDate(recordDateTimeEntered) // error: NSDate -> _ is not identical to NSTimeInterval
To compare two dates for equality, you can use the == operator in Swift (or the isEqualToDate: method in Objective-C). You can alternatively check that the timeIntervalSinceDate: between two dates is less than an acceptable threshold, to determine if two dates are roughly equal.

How to compare current and manual NSDate

there is a function where option NSDate is specified putting up manually.
How can I prevent execution of the function with the parameter of time, less than the current time?
I have tried this
if ([currentDate timeIntervalSinceDate: myTime] < 0)
but unfortunately sometimes it doesn't work
Thanks in advance.
Another option is to convert both the date to timeIntervals using timeIntervalSince1970
int interval1 = [date1 timeIntervalSince1970];
int interval2 = [date2 timeIntervalSince1970];
if(interval1 > interval2) //etc...
It sounds like you want to compare the current date with your custom date. More specifically, if the current date takes place after myTime, then the condition should be true.
if ([[NSDate date] compare: myTime] == NSOrderedDescending) {
NSLog(#"Current date is later than myTime");
}
To the opposite, you would check the comparison against NSOrderedAscending.

Time as a value to compare against my value

How can I display time as a number value for an if statement?
I'm trying to write an if statement where a set time is compared against my computed time for an event to occur. I just don't know how to write that time as a value. how can I write a time like 10am as a set time value for example.
Any help is much appreciated.
You can use the NSDate class and the timeIntervalSinceDate: method to compare the two times. You need to set up the two NSDate objects with the different times (and the same date) and then call that method to compare the two.
The easiest way to do this is probably to convert the time to a UNIX timestamp (unsigned integer representing the number of seconds which have elapsed since january 1st 1970)
convert both times to this format and compare.
time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
To compare two dates use:
- (NSComparisonResult)compare:(NSDate *)anotherDate
Here is the definition of NSComparisonResult:
enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

Compare two NSDates for same date/time [duplicate]

This question already has answers here:
How to compare two dates in Objective-C
(14 answers)
Closed 9 years ago.
Is date1 == date2 not a valid way to compare? If not, what is the correct alternative?
Here's my code:
- (NSDate*) dateWithNoTime {
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:self];
NSDate* dateOnly = [calendar dateFromComponents:components];
return dateOnly;
}
- (BOOL) sameDayAsDate:(NSDate*)dateToCompare {
NSDate *date1 = [self dateWithNoTime];
NSDate *date2 = [dateToCompare dateWithNoTime];
return date1 == date2; // HERE IS WHERE THINGS SEEM TO FAIL
}
You're comparing two pointer values. You need to use the NSDate comparison method like:
return ([date1 compare:date2] == NSOrderedSame);
As in the C language (of which Objective-C is a superset), the == (equality) operator compares two pointer values to see if they are equivalent (i.e. if two variables hold the same object). While this works in comparing primitive values (ints, chars, bools), it does not work on Objective-C objects, which might be equal in content, but differ in memory location (which is what the equality operator compares).
To check if two objects are equal, NSObject offers an -isEqual: method which you can use as a general statement (e.g. [date1 isEqual:date2]), and some classes choose to offer a more specific comparison method, such as -isEqualToDate: used to compare NSDates, or -isEqualToString: used to compare NSStrings. These methods cannot be used to compare primitive types (ints, for instance) because those are not objects, but will work on almost all objects.
You can't use == in Objective-C to compare object equality (it will take the C meaning, comparing pointers). Like other languages, you are simply comparing the object pointers.
The message you want is isEqualToDate:, aka [date1 isEqualToDate:date2]

finding NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

Is there a quick way in Objective-C of identifying NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?
I can't quite see anyway other than manually walking through each NSDate in the array and then using NSDateComponents to break out the hour/minute/second...Not even sure if there is a simple way to get the time from an NSDate in a fashion that represents a fraction of 24hours, as this might help a little. (e.g. 6pm would be 18/24 = 0.75 in this case)
There is no need to break in NSDateComponents.
NSTimeInterval interval = [date1 timeIntervalSinceDate:date2];
if (interval > 0) {
// date2 is earlier
} else {
// date1 is earlier
}
Now you can represent your target time(8 P.M., for example) with date2 and compare all dates of array with that.
Haven't tried this myself, but I guess
- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate
is what you're looking for.