2

i have one array something like below

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"
 )

all are NSString

now i want to make another array from this in which all years should be 2013 if that date is coming in current year and if date is already passed then change year to 2014?

in above sentence we are not considering year we consider only day and month and then see in current year is it already passed or not?

example if date is "12/15/1905" then convert it in another array like "12/15/2013" but if date is like "01/01/1990" then convert it in another array like "01/01/2014" bcoz it has already passed current date plz help thank you in advance :)

i will prefer to do something in which code apply for not only 2013 and 2014 it should apply for coming future too.

Prakash Desai
  • 168
  • 1
  • 9
  • we are not considering year we should only consider date and month and then check is that date and month has already passed or not? if passed or not passed we will do in accordance of that as i mentioned in the question – Prakash Desai Mar 13 '13 at 07:04
  • Hi Desai , If you want to get the solution you need to post your question more accurate.Currently it is very difficult to know what exactly you want. – Sawant Mar 13 '13 at 07:06
  • @DesaiPrakash: Check my answer as it is genric for any year. – Anoop Vaidya Mar 13 '13 at 08:39

4 Answers4

2

This code will do for you :

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);

Here is the Output:

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"
)
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • @Bhanu: reason please, so that i will come to know – Anoop Vaidya Mar 13 '13 at 09:01
  • Birthdates array contains the element like @"02/06". With this code the newBirthDates array will have element as @"02/06/2013", but required one there is @"02/06/2014"...Pls check my logic for this. – Bhanu Prakash Mar 13 '13 at 09:05
  • @Bhanu: Please copy my code and run in your system, I have shown output here, and thee is no error. – Anoop Vaidya Mar 13 '13 at 09:08
  • 1
    In your output element at index 4, "02/06/2013" is needs to be printed as "02/06/2014". pls see the requirement "now i want to make another array from this in which all years should be 2013 if that date is coming in current year and if date is already passed then change year to 2014?" – Bhanu Prakash Mar 13 '13 at 09:12
  • Oh shit @Bhanu: yes you are right, I coded correctly in my mac but forgot to paste, here is the revised one. – Anoop Vaidya Mar 13 '13 at 09:21
1

I take it you know how to use the NSDateFormatter to format dates, to change the year alone, just check this link NSDateComponents. Please have a look at this StackOverflow question on how to split the date into NSDateComponents.

Community
  • 1
  • 1
Premsuraj
  • 2,506
  • 1
  • 17
  • 16
1

This code is same as Anoop Vaidya code upto fetching all the dateStrings into one format. but the date comparision logic which fails in Anoop code is over comed here i.e., when count == 2..

Here is my edited code respect to 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);

This will work in all the scenarios...

Bhanu Prakash
  • 1,493
  • 8
  • 20
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.

Sawant
  • 395
  • 6
  • 22