简体   繁体   English

比较两个 NSDates 的月份和日期

[英]Compare the month and day of two NSDates

I need to setup an if/else statement that compares only the month and day of two NSDates.我需要设置一个 if/else 语句,只比较两个 NSDates 的月份和日期。 One of the dates is selected with a UIDatePicker and the other would be today's date.其中一个日期是用 UIDatePicker 选择的,另一个是今天的日期。 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).这是我目前设置它的方式,但它不起作用(else 语句总是触发)。 Not sure where to go from here:不知道从这里去哪里:

.h 。H

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

.m .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.我只需要匹配 NSDates 的月和日部分,因此我尝试将它们格式化为这些部分并比较字符串。 Problem is, I always get the "The dates don't match."问题是,我总是得到"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.不知道为什么,可以使用解释和正确的方法来解决这个问题。

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

Comparing strings could work but it's probably more efficient to use NSCalendar like this:比较字符串可以工作,但像这样使用 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
}

now, since iOS 8+, it's like :现在,从 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM