1

I need to setup an if/else statement that compares only the month and day of two NSDates. One of the dates is selected with a UIDatePicker and the other would be today's date. If the month and date of the two are the same, something happens, if not something else happens.

This is how I currently have it set up, but it's not working (the else statement always fires). Not sure where to go from here:

.h

@interface ViewController : UIViewController {
    UIDatePicker *datePicker;
    NSDate *pickedDate;
    NSDateFormatter *pickedDateFormat;
    NSString *pickedDateString;
    NSDate *todaysDate;
    NSDateFormatter *todaysDateFormat;
    NSString *todaysDateString;
    UILabel *label;
}

.m

- (IBAction)buttonToCompareDates:(id)sender {
    // Sets date from date picker
    pickedDate = [datePicker date];
    pickedDateFormat = [[NSDateFormatter alloc] init];
    [pickedDateFormat setDateFormat:@"MM/dd"];
    pickedDateString = [pickedDateFormat stringFromDate:pickedDate];

    // Sets today's date
    todaysDate = [NSDate date];
    todaysDateFormat = [[NSDateFormatter alloc] init];
    [todaysDateFormat setDateFormat:@"MM/dd"];
    todaysDateString = [todaysDateFormat stringFromDate:todaysDate];

    // if/else statement 
    if (todaysDateString == pickedDateString) {
         label.text = @"The dates match.";
    } else { 
         label.text = @"The dates don't match.";
    }
}

I was under the assumption that this would work, but it doesn't. I only need to match the month and day sections of the NSDates, therefore I tried to format them down to those parts and compare the strings. Problem is, I always get the "The dates don't match." sentence, even when the dates do match. Not sure why, could use an explanation and the right way to go about this.

2
  • as I know NSString can't compare with "==" .You will get warning "Direct comparison of a string literal has undefined behavior". try isEqualToString method
    – Huy Nghia
    Feb 20, 2015 at 7:23
  • @HuyNghia you are absolutely right because it is not possible to overload operator in Objective C. Feb 20, 2015 at 7:30

3 Answers 3

3

You have to use

if ([todaysDateString isEqualToString:pickedDateString]) {

instead of

if (todaysDateString == pickedDateString) {

Because you have to compare string contents instead of their pointers.

Also you can optimize you implementation by using the same Date Formatter:

- (IBAction)buttonToCompareDates:(id)sender {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd"];

    // Sets date from date picker
    pickedDate = [datePicker date];
    pickedDateString = [dateFormatter stringFromDate:pickedDate];

    // Sets today's date
    todaysDate = [NSDate date];
    todaysDateString = [dateFormatter stringFromDate:todaysDate];

    // if/else statement 
    if ([todaysDateString isEqualToString:pickedDateString]) {
         label.text = @"The dates match.";
    } else { 
         label.text = @"The dates don't match.";
    }
}
2
  • Hey, that solved it! Never knew that line existed, still new to Objective-C. Thanks for the quick reply.
    – Dick
    Feb 20, 2015 at 7:23
  • @Dick It would be much easer for you to learn Objective C if you have strong background in C language. Feb 20, 2015 at 7:27
3

Comparing strings could work but it's probably more efficient to use NSCalendar like this:

NSDate *date1, *date2;
NSDateComponents* components1 = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date1];
NSDateComponents* components2 = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date2];
if (components1.month == components2.month && components1.day == components2.day) {
    // same month and day
} else {
    // not same month and day
}
1
  • For iOS 8.0+ use NSCalendarUnitMonth|NSCalendarUnitDay as NSMonthCalendarUnit is deprecated as of iOS 8.0 Feb 18, 2016 at 17:45
1

now, since iOS 8+, it's like :

NSDateComponents* components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitMonth|NSCalendarUnitDay fromDate:dateArticle];
NSDateComponents* components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitMonth|NSCalendarUnitDay fromDate:frDateNow];

if (components1.month == components2.month && components1.day == components2.day) {
    // same month and day
} else {
    // not same month and day
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.