将字符串中的日期和日期与当前日期的月份和日期进行比较

时间:2015-08-20 11:18:33

标签: ios objective-c cocoa-touch date comparison

我有一个JSON对象有几个日期,例如:15/12/2005或14/12/2012。

我需要对今天的月和日进行比较;这一年并不重要。我这样做:

-(void)setSingle:(NSDictionary*)object name:(NSString*)name {

    NSMutableDictionary * content = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:name]];

    NSLog(@"%@", name);

    if(name == @"teste") {
        NSDate *dateReturn = [[NSDate alloc] init];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM"];

        NSString * tempDate;
        NSDictionary *dateObjectTemp = nil;
        NSDictionary * dateObject = [object objectForKey:name];

        for (NSDictionary*g in dateObject) {
            tempDate = [g objectForKey:@"dia"];
            dateObjectTemp = g;

            NSLog(@"Data: %@", tempDate);

            dateReturn = [dateFormatter dateFromString:tempDate];

            NSLog(@"Data: %@", dateReturn);
            [content setObject:dateObjectTemp forKey:tempDate];
        }

    } else {
        NSDate *dateReturn = [[NSDate alloc] init];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];

        NSString * tempDate;
        NSDictionary *dateObjectTemp = nil;
        NSDictionary * dateObject = [object objectForKey:name];

        for (NSDictionary*g in dateObject) {
            tempDate = [g objectForKey:@"dia"];
            dateObjectTemp = g;

            dateReturn = [dateFormatter dateFromString:tempDate];
            [content setObject:dateObjectTemp forKey:tempDate];
        }
    }

    NSDictionary* cont =content;
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:name];

    [[NSUserDefaults standardUserDefaults] setObject:cont forKey:name];

}

这会选择日期,但dataReturn会返回2000-12-15。我只想要日和月。

什么时候会比较下一个方法

-(void)showContent{

    if ( [self daysBetweenDate:currentDate andDate:self.dateLimit]>=0 ) {

        NSDictionary *santododia  = [super compareDate:currentDate name:@"santos"];
        [self defineContent:[santododia objectForKey:@"texto"]];

        self.urlShare = [santododia objectForKey:@"url"];
        self.tituloShare = [santododia objectForKey:@"titulo"];
    }else{
        [self.webView loadHTMLString: @"<font color='black' style='font-family:sans-serif;'>Conteúdo indisponível, escolha a data de hoje ou anterior</font>" baseURL:nil];
    }
}

虽然当前日期匹配,但测试未通过。

-(void) defineContent:(NSString *)string{

    string = [parentClass checkStringContent:string];
    NSLog(@"Teste: %@", string);
    if(string.length == 0){
       string = @"<font color='black' style='font-family:sans-serif;'>nothing</font>";
    } else {
        string =  [NSString stringWithFormat: @"%@ %@", string , @"<style>body {color:#000 !important} .day{margin-top:5px} .month{margin-top:5px}  </style>" ];
    }

    [self.webView loadHTMLString: string baseURL:nil];

}

2 个答案:

答案 0 :(得分:0)

要仅比较日期的某些部分,您应使用NSDateComponents。使用NSDate将您的字符串解析为NSDateFormatter

NSString * jsonDateString = @"15/12/2005";
NSDateFormatter * dF = [NSDateFormatter new];
// Format string must match date string!
dF.dateFormat = @"dd/MM/yyy";

NSDate * testDate = [dF dateFromString:jsonDateString];
NSDate * today = [NSDate date];

从那里开始,您使用NSCalendardecompose that date,今天就可以了解您感兴趣的内容:

NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents * todayComps = [cal components:NSCalendarUnitMonth|NSCalendarUnitDay
                                       fromDate:today];
NSDateComponents * testComps = [cal components:NSCalendarUnitMonth|NSCalendarUnitDay
                                      fromDate:testDate];

然后您可以比较这些日期组件对象:

[testComps isEqual:todayComps]

如果结果为YES,则两个日期具有相同的日期和月份,忽略日期的所有其他部分,包括年份。

答案 1 :(得分:-1)

尝试此比较日期 -

NSDate *minDate ;
NSDate *maxDate ;

NSComparisonResult compareStart = [minDate compare: self.selectedDate]; // date1 is 6/6/2011 00:00
NSComparisonResult compareEnd = [maxDate compare: self.selectedDate];   // date2 is 9/6/2011 00:00

if ( ( (compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame) ) && (compareEnd == NSOrderedDescending))
{
    NSLog(@"date is in the right range");
}
else {
    NSLog(@"Range is not valid .");
}