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.
Related
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
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;
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]
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.
I know I must be over-complicating this because it NSTimeInterval is just a double, but I just can't seem to get this done properly since I have had very little exposure to objective c. the scenario is as follows:
The data im pulling into the app contains two values, startTime and endTime, which are the epoch times in milliseconds. The variables that I want to hold these values are
NSTimeInterval *start;
NSTimeInterval *end;
I decided to store them as NSTimeIntervals but im thinking that maybe i ought to store them as doubles because theres no need for NSTimeIntervals since comparisons can just be done with a primitive. Either way, I'd like to know what I'm missing in the following step, where I try to convert from string to NSTimeInterval:
tempString = [truckArray objectAtIndex:2];
tempDouble = [tempString doubleValue];
Now it's safely stored as a double, but I can't get the value into an NSTimeInterval. How should this be accomplished? Thanks
You don't have to cast, you can just write this:
NSTimeInterval timeInterval = [[truckArray objectAtIndex:2] doubleValue];
The cast is needless, and extra casts just make your source code harder to update and change in the future because you've told the compiler not to type-check your casted expressions.
The variables that I want to hold these values are NSTimeInterval *start; NSTimeInterval *end;
Careful, NSTimeInverval is a typedef for a primitive C type, it is not an Objective-C object. I don't think you actually need pointers to these types in this scenario, so you should declare them like this:
NSTimeInverval start;
NSTimeInterval end;
You could be getting errors because in C, you cannot convert floating-point types to pointer-types.