【问题标题】:How to change year in date to current year?如何将日期中的年份更改为当前年份?
【发布时间】:2013-03-01 00:07:51
【问题描述】:

我有一个类似下面的数组

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 年的事情,它也应该适用于未来的未来。

【问题讨论】:

  • 我们不考虑年份,我们应该只考虑日期和月份,然后检查日期和月份是否已经过去?如果通过或不通过,我们将按照我在问题中提到的那样做
  • 嗨,Desai,如果您想获得解决方案,您需要更准确地发布您的问题。目前很难知道您到底想要什么。
  • @DesaiPrakash:检查我的答案,因为它在任何一年都是通用的。

标签: iphone ios date calendar


【解决方案1】:

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

【讨论】:

    【解决方案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"
    )
    

    【讨论】:

    • @Bhanu:请讲道理,好让我知道
    • Birthdates 数组包含类似 @"02/06" 的元素。使用此代码,newBirthDates 数组的元素将为 @"02/06/2013",但需要一个 @"02/06/2014"...请检查我的逻辑。
    • @Bhanu:请复制我的代码并在你的系统中运行,我在这里显示了输出,你没有错误。
    • 在索引 4 处的输出元素中,“02/06/2013”​​需要打印为“02/06/2014”。请参阅要求“现在我想从中创建另一个数组,如果该日期即将到来,并且如果日期已经过去,那么所有年份都应该是 2013 年,然后将年份更改为 2014 年?”
    • 哦该死@Bhanu:是的,你是对的,我在我的mac中正确编码但忘记粘贴,这是修改后的。
    【解决方案3】:

    我想你可以不使用dateFormatter直接检查。

    步骤: 首先使用 NSDate 找到当前日期。将其转换为字符串,并使用 NSDateFormatter 使格式与您拥有的数组相同。

    接下来通过条件一一比较。 用 "/" 分隔字符串。 if([myArray objectAtIndex:i]string 以@"/" objectAtIndex:0 分隔) 然后检查下一个... 根据需要设置条件

    我希望如果您这样做,只需更少的代码行数就可以实现您想要的。

    【讨论】:

      【解决方案4】:

      此代码与 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);
      

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

      【讨论】:

      • 你是真正的工程师,用你的触摸Miss或Mr.复制粘贴:)
      • @Anoop - 我在回答中明确提到,您的回答可以很好地格式化日期字符串,所以只有我接受了......:-)
      • 它不会进入 if(comResult == NSOrderedAscending) 怎么办。
      猜你喜欢
      • 2018-04-05
      • 2014-06-12
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多