ios - 如何对数组中的日期进行分组?
问题描述
我的日期格式是这样的“2019-12-03T05:58:11.317Z”。我想对相同的日期元素进行分组。我可以根据日期创建 UITableView 部分。这是数组,我有三个不同的日期,所以我想创建 3 个部分。这里的问题是newArrayList没有保存所有元素。
NSMutableArray * arrayList;
NSMutableArray *newArrayList;
//This is my date array
arrayList = [[NSMutableArray alloc] initWithObjects:@“2019-12-03T05:58:11.317Z”, @“2019-12-03T05:58:10.317Z”, @“2019-12-03T05:58:01.317Z”, @“2019-12-03T05:55:12.448Z”, @“2019-12-03T05:48:11.317Z”, @“2019-12-03T05:28:11.317Z”, @“2019-12-03T05:11:24.004Z”, @“2019-12-03T05:28:11.317Z”, @“2019-12-03T05:55:12.965Z”, @“2019-12-02T15:09:35.408Z”, @“2019-12-02T15:09:38.187Z”, @“2019-12-02T15:43:02.118Z”, @“2019-12-02T15:44:09.344Z”, @“2019-12-02T17:07:55.038Z”, @“2019-12-02T16:42:16.649Z”, @“2019-12-01T16:42:16.649Z”, nil];
newArrayList = [[NSMutableArray alloc]init];//Here I want to same date elements
NSDate *fromDate;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int i=0; i<arrayList.count; i++) {
NSDate *dt1 = fromDate;
NSDate *dt2 = [self getLocalDateTimeFromUTC2:[arrayList objectAtIndex:i]];
if (i != 0) {
if ([self isSameDay:dt1 otherDay:dt2] == YES) {
[tempArray addObject:t];
} else {
[newArrayList addObject:tempArray];
// [tempArray removeAllObjects];
}
}
fromDate = dt2;
}
//To get required date formate
-(NSDate *)getLocalDateTimeFromUTC2:(NSString *)strDate
{
// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"]; //@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
// NSLog(@"%@", t.dateAndTime); //2019-12-02T15:09:38.187Z
NSDate *date = [dateFormatter dateFromString:strDate]; // create date from string
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date2 = [dateFormat dateFromString:timestamp];
// NSLog(@"%@", date2);
return date2;
}
//To compare two dates
- (BOOL)isSameDay:(NSDate*)date1 otherDay:(NSDate*)date2 {
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
return [comp1 day] == [comp2 day] && [comp1 month] == [comp2 month] && [comp1 year] == [comp2 year];
}
解决方案
尝试这个:
NSMutableDictionary *dictByDate = [NSMutableDictionary new];
NSMutableArray *newArr = [NSMutableArray new] ;
for(NSString *strDate in arrayList)
{
// convert date to proper formate (if needed)
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *dateNew = [format dateFromString:strDate];
[format setDateFormat:@"MMMM-dd-yyyy"];
NSString *finalDateString = [format stringFromDate:dateNew];
NSLog(@"%@",finalDateString) ;
NSMutableArray *arrWithSameDate = dictByDate[finalDateString];
if(! arrWithSameDate)
{
arrWithSameDate = [NSMutableArray new];
dictByDate[finalDateString] = arrWithSameDate;
}
[arrWithSameDate addObject: strDate];
}
[newArr addObjectsFromArray:[dictByDate allValues]] ;
NSLog(@"group data: %@", newArr);
推荐阅读
- javascript - Angular JS:Karma 未安装在项目文件夹中。呃!代码完整性
- javascript - Firebase 存储:索引 0 处的“put”中的参数无效:预期的 Blob 或文件
- mongodb - MongoDB查询多个嵌套字段的公共字段
- python - Python:ValueError 和 XLRDError 的异常处理
- python - 有条件地更改 Python Marshmallow 中的字段属性
- python - 根据列上的条件合并 2 个数据框
- php - 在 params 数组中回显文件内容
- javascript - 对具有对象数组的复杂对象动态执行 .join
- python - 如何对执行 SQL 的函数进行单元测试而不影响 Python 中的数据库?
- powershell - sudent,New-ADUser 多用户创建脚本的名称无效