I was wondering how I could use NSDate within an if statement - I want to update a UILabel depending what the date is, currently I have the following code to determine the date but don't know how to actually get this within an if statement.
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [formatter stringFromDate:date];
dateLabel.text = dateString;
if (dateString == @"25/05/2012") {
NSLog(@"It's the 25th!");
}
else {
NSLog(@"Not it's not...");
}
Thanks a lot!
If you want to compare two strings use isEqualToString:
if ([dateString isEqualToString:@"25/05/2012"]) {
NSLog(@"It's the 25th!");
}
else {
NSLog(@"Not it's not...");
}
isEqualToString: in NSString class reference
if you want to compare two NSDate
use isEqualToDate:
[date1 isEqualToDate:date2]
To compare strings you can use isEqualToString:
not ==
(with this you're comparing the pointers).
To compare dates you can use isEqualToDate:
.
Depending on what you actually want to achieve you can use next calls:
NSDate:
- (BOOL)isEqualToDate:(NSDate *)anotherDate
- (NSComparisonResult)compare:(NSDate *)anotherDate
NSString:
- (BOOL)isEqualToString:(NSString *)aString
As has been noted, ==
checks for pointer equality. If you want to match the contents of an NSString
or NSDate
, or any other class of object, check that class' documentation for isEqual...
and compare...
methods. ( NSDate
has isEqualToDate:
and compare:
; NSString
has isEqualToString:
and compare:
as well as several more specialized comparison methods.
However, depending on just what your aim is in matching dates, comparing NSDates
with isEqualToDate:
or checking the result of compare:
against NSOrderedSame
might not do what you want. An NSDate
doesn't represent a whole calendar day but rather a specific moment in time. So if you have an NSDate
representing 5/25/2012 1:53:13 AM PDT
and one representing 5/25/2012 1:55:01 AM PDT
, they will be equal or compare as NSOrderedSame
.
What you're going for with the NSDateFormatter
and string comparison will sort of work for checking whether two NSDates
represent the same calendar day (presuming you compare string contents with isEqualToString:
instead of using ==
), but it's sort of kludgy and will fail in certain edge cases that are more common than you think. Apple provides APIs specifically for such comparisons; there's a section in the Date and Time Programming Guide that explains them well.
NSDate provides
- (BOOL)isEqualToDate:(NSDate *)anotherDate
- (NSComparisonResult)compare:(NSDate *)anotherDate
Here is a category (found here http://webd.fr/637-comparer-deux-nsdate ) that offers a neat way to compare NSDates:
#import <Foundation/Foundation.h>
@interface NSDate (Compare)
-(BOOL) isLaterThanOrEqualTo:(NSDate*)date;
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date;
-(BOOL) isLaterThan:(NSDate*)date;
-(BOOL) isEarlierThan:(NSDate*)date;
//- (BOOL)isEqualToDate:(NSDate *)date; already part of the NSDate API
@end
And the implementation:
#import "NSDate+Compare.h"
@implementation NSDate (Compare)
-(BOOL) isLaterThanOrEqualTo:(NSDate*)date {
return !([self compare:date] == NSOrderedAscending);
}
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date {
return !([self compare:date] == NSOrderedDescending);
}
-(BOOL) isLaterThan:(NSDate*)date {
return ([self compare:date] == NSOrderedDescending);
}
-(BOOL) isEarlierThan:(NSDate*)date {
return ([self compare:date] == NSOrderedAscending);
}
@end
Simple to use:
if([aDateYouWant ToCompare isEarlierThanOrEqualTo:[NSDate date]]) // [NSDate date] is now
{
// do your thing ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.