1

I have an array containing birth dates like the one below:

 Array(
"11/07/2013",
"07/10/2013",
"20/02/2013"
)

Now I want to make a new array based on whether or not the date has passed. Writing this question in 2013, if a current date has passed then we will change that date's year to 2014. If it hasn't passed then we will have it stay the 2013 date.

For example:

NewArray(
 "11/07/2013",    no change cus this date hasnt passed yet
 "07/10/2013",     no change same as above
 "20/02/2014"     **as date has already passed thats why 2014**

I'm using the following code for this

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSString *curYear = [dateFormatter stringFromDate:[NSDate date]];
NSString *nextYear = [NSString stringWithFormat: @"%d", ([curYear intValue] + 1)];
for(int i = 0; i < [_newlymadeArray count]; i++)
{
    NSString *dateStr = [_newlymadeArray objectAtIndex:i];
    NSComparisonResult comResult = [[dateFormatter dateFromString:dateStr] compare:    [NSDate date]];
    if(comResult == NSOrderedAscending)
    {
        [dateStr stringByReplacingOccurrencesOfString:curYear withString:nextYear];
        [_newlymadeArray replaceObjectAtIndex:i withObject:dateStr];
         NSLog(@"_newlymadeArray%@",_newlymadeArray);
    }
NSLog(@"_newlymadeArray%@",_newlymadeArray);

This is however what I get when I NSLog _newlymadeArray:

 after replacing (
 "11/07/2013",
 "07/10/2013",
 "20/02/2013"       
 )

At index 2 it should be "20/02/2014" instead of the 2013 date. What might cause my problem and how can I solve it?

Bart
  • 19,692
  • 7
  • 68
  • 77
Prakash Desai
  • 168
  • 1
  • 9
  • hey you can use my code which i have give in your previous Question if date < 0 then change year – Pratik Mar 13 '13 at 11:46
  • 1
    Perhaps the [Calendrical Calculations](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1) section of the Apple [Date and Time Programming Guide](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039-SW1) will be of help to you. – Elliott Mar 13 '13 at 11:48
  • Check the datre with NSOrderedAscending and NSOrdereddescending and make change – 08442 Mar 13 '13 at 11:48
  • there are 4 answer non of them is your Pratik can you plz give me answer here? – Prakash Desai Mar 13 '13 at 11:48
  • http://stackoverflow.com/questions/15377466/finding-out-days-remain-from-array-of-dates/15377946#15377946 look second answer – Pratik Mar 13 '13 at 11:50
  • thats was for finding difference thats totally other story pratik thats solved please read this question again by the way you really helped me there hope to see help form you here too :) – Prakash Desai Mar 13 '13 at 11:53
  • in this you have to put just if condition. – Pratik Mar 13 '13 at 11:55
  • Why aren't you holding an array of `NSDate` objects? This is a more appropriate why of holding this data. – trojanfoe Mar 13 '13 at 11:56

4 Answers4

2

I've made some modifications to your code, and it is working as you want.

In my code I've compared date, which is in Ascending form of current date. If it satisfies the condition, then I've fetched YEAR from matched date, by the DateFormatter "yyyy". Then I simply increment this year by 1, and replace this year in old Date, which is "20/02/2013" to "20/02/2014"

array = [[NSMutableArray alloc] initWithObjects:@"11/07/2013",@"07/10/2013",@"20/02/2013", nil];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
for(int i = 0; i < [array count]; i++)
{
    NSString *dateStr = [array objectAtIndex:i];
    NSComparisonResult comResult = [[dateFormatter dateFromString:dateStr] compare:    [NSDate date]];
    if(comResult == NSOrderedAscending)
    {
        NSDateFormatter *yrFormatter = [[NSDateFormatter alloc] init];
        [yrFormatter setDateFormat:@"yyyy"];
        NSString *curYear = [yrFormatter stringFromDate:[NSDate date]];
        NSString *nextYear = [NSString stringWithFormat: @"%d", ([curYear intValue] + 1)];

        NSLog(@"%@",curYear);
        NSLog(@"%@",nextYear);

        dateStr = [dateStr stringByReplacingOccurrencesOfString:curYear withString:nextYear];
        NSLog(@"%@",dateStr);
        [array replaceObjectAtIndex:i withObject:dateStr];
        NSLog(@"_newlymadeArray%@",array);
    }
    NSLog(@"_newlymadeArray%@",array);
}

This seems to be working perfectly, so I hope it helps you.

Bart
  • 19,692
  • 7
  • 68
  • 77
Dhruvik
  • 984
  • 4
  • 18
  • damn man you are expert man tell me when you started to learn objective c? tell me how long its been? – Prakash Desai Mar 13 '13 at 12:13
  • thanx man, bt m not that much expert. trying to improve knowledge day by day. i am workin on iOS since 7 months only. @DesaiPrakash – Dhruvik Mar 13 '13 at 12:41
  • 1
    @Dhruvik Just a small request: instead of just posting a block of code, could you add a small clarification on what the problem was and how you solved it? It would certainly improve your answer. – Bart Apr 03 '13 at 16:04
  • 1
    @Bart sure dude., i've added some explanations in my answer. plz go through it. Thanks. – Dhruvik Apr 05 '13 at 07:32
1

Plese dear try to use this one.I think this one may help

 NSCalendar*       calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [[NSDateComponents alloc] init];
    components.year = 1;
    NSDate* newDate = [calendar dateByAddingComponents: components toDate:@"YourDate" options: 0];

Otherwise you can use this one code.

   if (comResult == NSOrderedSame)
   {
       NSCalendar*       calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [[NSDateComponents alloc] init];
    components.year = 1;

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd/MM/yyyy"];

    NSDate *date = [formatter dateFromString:@"11/07/2013"];

    NSDate* newDate = [calendar dateByAddingComponents: components toDate:date options: 0];
   // here replace your array object with this "newDate"

   }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Compare your array date to today's date with NSDate compare function. Here are the details:

NSString *arrayDateString = @"20/02/2013" // fetch this string from your array
NSDate *todaysDate = [NSDate date];

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
NSDate* d = [df dateFromString:arrayDateString];

// now compare this date (d) with todaysDate using NSDate function
if ([d compare:todaysdate]== NSOrderedAscending)  
{//write your code here}

If it results NSOrderedAscending, then it means array date is earlier than today's date.

So for that date, update year incremented by one using NSDateComponents:

NSDateComponents *dayComponent = [[[NSDateComponents alloc] init] autorelease];
dayComponent.year = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
dateToBeIncremented = [theCalendar dateByAddingComponents:dayComponent toDate:dateToBeIncremented options:0];

Or you can use NSDate function itself:

NSDate *now = arrayDate;
int yearsToAdd = 1;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*365*year];

But second option is not full-proof - because of leap year problem.

Hope this two options help you, for solving your issue.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
0
NSArray * array = [[NSArray alloc]   initWithObjects:@"11/07/2013",@"07/10/2013",@"20/02/2013", nil];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSMutableArray *output = [NSMutableArray new];

for(int i = 0; i < [array count]; i++)
{
    NSString *dateStr = [array objectAtIndex:i];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    if ([date compare:now] == NSOrderedAscending)
    {

        [components setYear:1];
        date = [calendar dateByAddingComponents:components toDate:date options:0];

    }
    dateStr = [dateFormatter stringFromDate:date];
    [output addObject:dateStr];

}
NSLog(@"Result : %@",output);
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110