2013-03-13 87 views
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”,则将其转换为另一个数组,如“ 2014" 年1月1日 bcoz它已经通过了当前的日期 plz帮助预先感谢您:)

,我会更喜欢做其中的代码申请,不仅2013年和2014年的东西,应该申请来未来太。

+0

我们不考虑年份,我们应该只考虑日期和月份,然后检查日期和月份是否已经过去了?如果通过或没有通过,我们将按照我所提的问题 – 2013-03-13 07:04:56

+0

Hi Desai,如果你想得到解决方案,你需要发布你的问题更准确。目前很难知道你想要什么。 – Sawant 2013-03-13 07:06:23

+0

@DesaiPrakash:查看我的答案,因为它是任何一年的genric。 – 2013-03-13 08:39:00

回答

1

此代码与Anoop Vaidya代码相同,可将所有dateStrings提取为一种格式。但失败在Anoop代码日期比较的逻辑是在这里COMED即当计数== 2 ..

这是我编辑的代码对于Anoop

NSArray *[email protected][ 
     @"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); 

这将在所有的情况下工作。 ..

+0

你是真正的工程师,复制粘贴与你的触摸小姐或先生:) – 2013-03-13 09:09:43

+0

@Anoop - 我在我的答案中明确提到,你的答案是最好格式化日期字符串,所以只有我采取了.. :-) – 2013-03-13 09:18:16

+0

感谢您指出的错误,Obliged :) – 2013-03-13 09:22:04

1

我认为你知道如何使用NSDateFormatter格式化日期,单独更改年份,只需检查此链接NSDateComponents。请查看此StackOverflow question关于如何将日期分成NSDateComponents。

+0

好吧,我谢谢你的回答 – 2013-03-13 07:06:16

2

此代码为你会做什么:

NSArray *[email protected][ 
         @"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" 
) 
+0

这个代码将失败,当计数== 2.请检查。 – 2013-03-13 08:59:52

+0

@Bhanu:请原谅,这样我才会知道 – 2013-03-13 09:01:11

+0

生日数组包含@“02/06”这样的元素。使用这段代码newBirthDates数组的元素为@“02/06/2013”​​,但需要一个@“02/06/2014”...请检查我的逻辑。 – 2013-03-13 09:05:41

0

我认为你可以直接检查,而无需使用dateFormatter。

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

接下来通过作出条件逐一比较。 用“/”分隔字符串。 如果([myArray的objectAtIndex:我]字符串单独由@“/” objectAtIndex:0) 和检查下一个... 使病情,只要你想

我希望,如果你不喜欢这样用更少代码行可以实现你想要的。

相关问题