比较两个 NSDates 的月份和日期

Compare the month and day of two NSDates

我需要设置一个 if/else 语句来比较两个 NSDates 的月份和日期。其中一个日期是使用 UIDatePicker 选择的,另一个是今天的日期。如果两者的月份和日期相同,则会发生某些事情,否则会发生其他事情。

这就是我目前的设置方式,但它不起作用(else 语句总是触发)。不确定从这里去哪里:

.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.";
    }
}

我原以为这会奏效,但事实并非如此。我只需要匹配 NSDates 的月份和日期部分,因此我尝试将它们格式化为这些部分并比较字符串。问题是,我总是得到 "The dates don't match." 句子,即使日期匹配。不知道为什么,可以使用解释和正确的方法来解决这个问题。

你必须使用

if ([todaysDateString isEqualToString:pickedDateString]) {

而不是

if (todaysDateString == pickedDateString) {

因为你必须比较字符串内容而不是它们的指针。

您还可以使用相同的日期格式化程序优化您的实现:

- (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.";
    }
}

比较字符串可以工作,但像这样使用 NSCalendar 可能更有效:

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
}

现在,因为 iOS 8+,就像:

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
}