Ios 如何从日期数组中获取日期?
我有一个包含不同日期的数组Ios 如何从日期数组中获取日期?,ios,arrays,date,nsdateformatter,Ios,Arrays,Date,Nsdateformatter,我有一个包含不同日期的数组 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", "2
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"
)
我想从这个数组中得到另一个数组,它只包含(2015-03-222015-03-232015-03-24
)
请帮助我解决此问题假设您只需要一个只包含日期部分的新数组(删除时间信息),您可以执行以下操作
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];
}
现在,您将得到一个新的可变数组-另一个数组
,其中包含您所需格式的日期字符串
我希望这就是你要找的
干杯
Jim我想可能是重复的您正在寻找一个新的日期数组,尽管这次只包含日期(没有时间信息)?