-1

I have an array which contains different dates

    NSLog(@"Array of dates contains %@", [_dates valueForKey:@"date"]);
Array of dates contains (
    "2015-03-22 08:45:00 +0000",
    "2015-03-22 19:00:00 +0000",
    "2015-03-23 12:15:00 +0000",
    "2015-03-23 13:30:00 +0000",
    "2015-03-24 07:30:00 +0000",
    "2015-03-24 13:30:00 +0000",
    "2015-03-25 08:45:00 +0000",
    "2015-03-25 09:00:00 +0000",
    "2015-03-25 09:30:00 +0000",
    "2015-03-26 08:00:00 +0000",
    "2015-03-26 10:00:00 +0000",
    "2015-03-27 09:00:00 +0000",
    "2015-03-27 19:00:00 +0000",
    "2015-03-28 08:45:00 +0000"
)

I want to get from this array another array, which contains just (2015-03-22, 2015-03-23, 2015-03-24) Please help me solve this problem

Huy Nghia
  • 996
  • 8
  • 22
Vitalyz123
  • 189
  • 2
  • 10
  • possible duplicate of [filtering NSArray into a new NSArray in objective-c](http://stackoverflow.com/questions/110332/filtering-nsarray-into-a-new-nsarray-in-objective-c) – Rengers Mar 22 '15 at 16:44
  • I take it you're looking for a new array of dates, though this time only containing the date (without the time info)? – Jim Tierney Mar 23 '15 at 09:00

1 Answers1

0

Assuming you simply want a new array with the date part only (removing the time info), you can do the following

NSArray *originalArray = [_dates valueForKey:@"date"];

//Create an empty mutableArray to add the new formatted date strings into
NSMutableArray *anotherArray = [NSMutableArray alloc];

//Choosing the format of the date you require from the NSDate object
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd";

    for (NSDate *date in originalArray) {

     //This line will make a string from the date which you specified to only use the yyyy-MM-dd etc.
     NSString *datesWithoutTimeString = [formatter stringFromDate:date];

    [anotherArray addObject:datesWithoutTimeString];
    }

Now you will be left with a new MutableArray - anotherArray containing strings of the dates in the format you require.

I hope this is what you're looking for.

Cheers

Jim

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48