【问题标题】:Compare month and day of date in string to month and day of the current date比较字符串中日期的月份和日期与当前日期的月份和日期
【发布时间】:2015-08-20 11:18:32
【问题描述】:

我有一个包含多个日期的 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];

}

【问题讨论】:

  • 设置日期格式为 [dateFormatter setDateFormat:@"dd/MM/yyyy"];然后就可以正常工作了。
  • 但我不想使用年份。请允许我详细说明。我有一个文本 2015 年 12 月 15 日和 2002 年 16 月 12 日的另一天,如果您注意到不同的年份,但重要的是日期和月份,永远将显示在 16 年 12 月的所有日子文本,与年份无关

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


【解决方案1】:

要仅比较日期的一部分,您应该使用NSDateComponents。使用NSDateFormatter 将您的字符串解析为NSDates:

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,那么这两个日期具有相同的日期和月份,忽略日期的所有其他部分,包括年份。

【讨论】:

    【解决方案2】:

    试试这个比较日期 -

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2018-07-12
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多