2

I have array which is like as below

_combinedBirthdates(
    "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 elements are NSString in above NSArray.

How can I make another array which contains number of days remain for particular date something like this

_newlymadeArray(
    "125",
    "200",
    "50",
    "500",
    "125",
  and so on
)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Prakash Desai
  • 168
  • 1
  • 9

3 Answers3

3
unsigned int unitFlags =  NSDayCalendarUnit;
NSCalendar *currCalendar = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateStyle:NSDateFormatterShortStyle];

NSMutableArray * _newlymadeArray = [[NSMutableArray alloc] init];
for (NSString * str in _combinedBirthdates){

        NSDate * toDate = [dateFormatter dateFromString:str];
        NSDateComponents *daysInfo = [currCalendar components:unitFlags fromDate:[NSDate date]  toDate:toDate  options:0];
        int days = [daysInfo day];
        [_newlymadeArray addObject:[NSString stringWithFormat:@"%d",days]];

    }

You need to iterate through the first array and get the date in NSDate and use the info to get the difference in days from the current date to the next date in the array.

You will have to add the necessary checks and this is untested code.

Praveen S
  • 10,355
  • 2
  • 43
  • 69
3

Use this algorithm:

  1. Obtain the current year
  2. Convert each date from your array to a date in the current year. For example, "03/02/1990" becomes "03/02/2013"
  3. If the date from the step 2 is before the current date, advance its year by one (i.e. "03/02/2013" becomes "03/02/2014")
  4. Using a technique from this question, find the number of days till the date from step 3. It will be less than the number of days in a year.
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Try this code snap.

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSDate *currentDate = [formatter dateFromString:@"03/12/2013"];
NSTimeInterval srcInterval = [currentDate timeIntervalSince1970];

NSArray *_combinedBirthdates = @[@"03/15/2013", @"05/15/2013"];
NSMutableArray *_newlymadeArray = [NSMutableArray array];
const NSInteger SECONDS_PER_DAY = 60 * 60 * 24;

for (NSString *one in _combinedBirthdates) {
    NSDate *destDate = [formatter dateFromString:one];
    NSTimeInterval destInterval = [destDate timeIntervalSince1970];
    NSInteger diff = (destInterval - srcInterval) / SECONDS_PER_DAY;
    [_newlymadeArray addObject:[NSString stringWithFormat:@"%d", diff]];
}

That's it! :)

Pei
  • 11,452
  • 5
  • 41
  • 45
  • NSDate *currentDate = [formatter dateFromString:@"03/12/2013"]; how to add something else instead of 3/12/2013 so it works everyday? – Prakash Desai Mar 12 '13 at 10:36
  • 0, 0, "-8247", "-8494", "-15775", "-15775", "-8411", "-8603", "-15775", "-15775", "-15775", "-15775", "-8229", "-7637", "-39169", "-8611", "-9288", "-15775", "-8639", "-9485", "-8993", "-8451", "-15775", "-2071", "-887", "-8314" this is what i got in _newlymadearray – Prakash Desai Mar 12 '13 at 10:45