2

我有一个数组,如下所示

Birthdates(
"03/12/2013",
"03/12/2013",
"08/13/1990",
"12/09/1989",
"02/06",
"09/08",
"03/02/1990",
"08/22/1989",
"03/02",
"05/13",
"10/16",
"07/08",
"08/31/1990",
"04/14/1992",
"12/15/1905",
"08/14/1989",
"10/07/1987",
"07/25",
"07/17/1989",
"03/24/1987",
"07/28/1988",
"01/21/1990",
"10/13"
 )

都是 NSString

现在我想从中制作另一个数组,如果该日期即将到来,并且如果日期已经过去,那么所有年份都应该是 2013 年,然后将年份更改为 2014 年?

在上面的句子中,我们没有考虑年份,我们只考虑日和月,然后看看今年是否已经过去了?

例如,如果日期为“12/15/1905”,则将其转换为另一个数组,如“12/15/2013”​​,但如果日期为“01/01/1990”,则将其转换为另一个数组,如“01/01/2014” “因为它已经过了当前日期,请帮助提前谢谢你:)

我更愿意做一些代码不仅适用于 2013 年和 2014 年的事情,它也应该适用于未来的未来。

4

4 回答 4

2

此代码将为您做:

NSArray *Birthdates=@[
                      @"03/12/2013",
                      @"03/12/2013",
                      @"08/13/1990",
                      @"12/09/1989",
                      @"02/02",
                      @"09/08",
                      @"03/02/1990",
                      @"08/22/1989",
                      @"03/02"];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger currentYear = [components year];

NSMutableArray *newBirthDates=[NSMutableArray new];

for(NSString *str in Birthdates){
    NSArray *array=[str componentsSeparatedByString:@"/"];
    if(array.count==2){
        NSString *tempString=[NSString stringWithFormat:@"%@/%d",str,currentYear];
        NSDateFormatter *dateFormatter=[NSDateFormatter new];
        [dateFormatter setDateFormat:@"MM/dd/yyyy"];
        NSDate *date=[dateFormatter dateFromString:tempString];
        if ([[NSDate date] isGreaterThanOrEqualTo:date]) {
            NSLog(@"passed->%@ *** %@",date, str);
            [newBirthDates addObject:[NSString stringWithFormat:@"%@/%d",str,currentYear+1]];
        }
        [newBirthDates addObject:tempString];
    }
    else{
        NSString *dateString=[NSString stringWithFormat:@"%@/%@/%d",array[0],array[1],currentYear];
        NSDateFormatter *dateFormatter=[NSDateFormatter new];
        [dateFormatter setDateFormat:@"MM/dd/yyyy"];
        NSDate *date=[dateFormatter dateFromString:dateString];
        if ([[NSDate date] isGreaterThanOrEqualTo:date]) {
            dateString=[NSString stringWithFormat:@"%@/%@/%d",array[0],array[1],currentYear+1];
        }

        [newBirthDates addObject:dateString];
    }
}
NSLog(@"%@",newBirthDates);

这是输出:

DocumentBased[6632:303] (
    "03/12/2014",
    "03/12/2014",
    "08/13/2013",
    "12/09/2013",
    "02/02/2014",
    "09/08/2013",
    "03/02/2014",
    "08/22/2013",
    "03/02/2014"
)
于 2013-03-13T07:09:08.383 回答
1

我认为您知道如何使用 NSDateFormatter 来格式化日期,单独更改年份,只需检查此链接NSDateComponents。请查看有关如何将日期拆分为 NSDateComponents 的StackOverflow 问题。

于 2013-03-13T07:05:03.267 回答
1

此代码与将所有日期字符串提取为一种格式的 Anoop Vaidya 代码相同。但是在 Anoop 代码中失败的日期比较逻辑在这里结束了,即当 count == 2..

这是我对 Anoop 的编辑代码

    NSArray *Birthdates=@[
        @"03/12/2013",
        @"03/12/2013",
        @"08/13/1990",
        @"12/09/1989",
        @"02/06",
        @"09/08",
        @"03/02/1990",
        @"08/22/1989",
        @"03/02"];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy"];
        NSString *curYear = [dateFormatter stringFromDate:[NSDate date]];
        NSString *nextYear = [NSString stringWithFormat: @"%d", ([curYear intValue] + 1)];
        [dateFormatter setDateFormat:@"MM/dd/yyyy"];

        NSMutableArray *newBirthDates=[NSMutableArray new];
        for(NSString *str in Birthdates){
            NSArray *array=[str componentsSeparatedByString:@"/"];
            if(array.count==2){
                [newBirthDates addObject:[NSString stringWithFormat:@"%@/%@",str,curYear]];
            }
            else{
                [newBirthDates addObject:[NSString stringWithFormat:@"%@/%@/%@",array[0],array[1],curYear]];
            }
        }


        for(int i = 0; i < [newBirthDates count]; i++)
        {
            NSString *dateStr = [newBirthDates objectAtIndex:i];
            NSComparisonResult comResult = [[dateFormatter dateFromString:dateStr] compare:[NSDate date]];
            if(comResult == NSOrderedAscending)
            {
                [dateStr stringByReplacingOccurrencesOfString:curYear withString:nextYear];
                [newBirthDates replaceObjectAtIndex:i withObject:dateStr];
            }
        }

        NSLog(@"%@",newBirthDates);

这将适用于所有场景......

于 2013-03-13T08:51:58.953 回答
0

I think You can check it directly without using dateFormatter.

Steps to do: First find the current date with NSDate.Convert it into string and make the format as same the array you have, by using NSDateFormatter.

Next compare one by one by making condition . Separate the string by "/" . if([myArray objectAtIndex:i]string separate by@"/" objectAtIndex:0 ) and the check the next one ... Make the condition as you want

I hope if you do like this with less lines of code you can achieve what you want.

于 2013-03-13T07:13:56.863 回答