我正在尝试将小时和分钟添加到日期数组以下是我的代码,我已将旧数组的 NSLog 和数组之后
NSDate* date= _timePicker.date;
NSDateFormatter *formatter=[[[NSDateFormatter alloc]init]autorelease];
[formatter setDateFormat:@"hh:mm"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setTimeStyle:NSDateFormatterLongStyle];
NSDateFormatter *formatter1=[[[NSDateFormatter alloc]init]autorelease];
[formatter1 setDateFormat:@"dd:MM:yyyy"];
[formatter1 setTimeZone:[NSTimeZone systemTimeZone]];
[formatter1 setTimeStyle:NSDateFormatterLongStyle];
NSDateFormatter *formatter3=[[[NSDateFormatter alloc]init]autorelease];
[formatter3 setDateFormat:@"dd-MM-yyyy hh:mm"];
[formatter3 setTimeZone:[NSTimeZone systemTimeZone]];
[formatter3 setTimeStyle:NSDateFormatterLongStyle];
NSString *timeselected =[formatter stringFromDate:date];
NSLog(@"we have picked time %@",timeselected);
NSDate *date2 = [formatter dateFromString:timeselected];
// NSLog(@"date2%@",dateSelected );
NSLog(@"old%@",delegate.notificationBirthDateArray);
old(
"15/08/2013",
"15/07/2014",
"15/07/2014",
"07/06/2014",
"15/01/2014",
"27/01/2014",
"20/01/2014",
"22/06/2014",
"29/08/2013",
"15/06/2014",
"16/07/2013"
)
for (int i=0; i<[delegate.notificationBirthDateArray count]; i++) {
NSDate *date1 = [formatter1 dateFromString:[delegate.notificationBirthDateArray objectAtIndex:i]];
// NSLog(@"date2%@",date2);
// NSLog(@"array%@",date1);
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Extract date components into components1
NSDateComponents *components1 = [gregorianCalendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
fromDate:date1];
// Extract time components into components2
NSDateComponents *components2 = [gregorianCalendar components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:date2];
// Combine date and time into components3
NSDateComponents *components3 = [[NSDateComponents alloc] init];
[components3 setYear:components1.year];
[components3 setMonth:components1.month];
[components3 setDay:components1.day];
[components3 setHour:components2.hour];
[components3 setMinute:components2.minute];
// when i nslog this components they are printed perfectly
// Generate a new NSDate from components3.
NSDate *combinedDate = [gregorianCalendar dateFromComponents:components3];
// combinedDate contains both your date and time!
NSString *lastdate =[formatter3 stringFromDate:combinedDate];
NSLog(@"combinedDate%@",lastdate);
[delegate.notificationBirthDateArray removeObjectAtIndex:i];
[delegate.notificationBirthDateArray insertObject:lastdate atIndex:i];
}
NSLog(@"new%@",delegate.notificationBirthDateArray);
new(
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30",
"6:00:00 PM GMT+05:30"
)
问题是获得的时间以您在上面看到的正确方式添加,但我的日期去哪儿了?他们也应该在那里。
请帮我解决一下这个。
edited and imporved code but still same problem
NSArray*arr = [NSArray arrayWithArray:delegate.notificationBirthDateArray];
NSDate* date= _timePicker.date;
NSDateFormatter *formatter=[[[NSDateFormatter alloc]init]autorelease];
[formatter setDateFormat:@"hh:mm"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setTimeStyle:NSDateFormatterLongStyle];
NSDateFormatter *formatter1=[[[NSDateFormatter alloc]init]autorelease];
[formatter1 setDateFormat:@"dd/MM/yyyy"];
NSDateFormatter *formatter3=[[[NSDateFormatter alloc]init]autorelease];
[formatter3 setDateFormat:@"dd/MM/yyyy hh:mm a"];
[formatter3 setTimeZone:[NSTimeZone systemTimeZone]];
[formatter3 setTimeStyle:NSDateFormatterLongStyle];
NSString *timeselected =[formatter stringFromDate:date];
NSLog(@"we have picked time %@",timeselected);
NSDate *date2 = [formatter dateFromString:timeselected];
// NSLog(@"date2%@",dateSelected );
NSLog(@"old%@",delegate.notificationBirthDateArray);
for (int i=0; i<arr.count; i++) {
NSString *datefromarray = [[NSString alloc]initWithString:[delegate.notificationBirthDateArray objectAtIndex:i]];
NSDate *date1 = [formatter1 dateFromString:datefromarray];
// NSLog(@"hour time %@",[delegate.notificationBirthDateArray objectAtIndex:i]);
NSLog(@"hour time %@",date2);
NSLog(@"date%@",date1);
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Extract date components into components1
NSDateComponents *components1 = [gregorianCalendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
fromDate:date1];
// Extract time components into components2
NSDateComponents *components2 = [gregorianCalendar components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:date2];
// Combine date and time into components3
NSDateComponents *components3 = [[NSDateComponents alloc] init];
[components3 setYear:components1.year];
[components3 setMonth:components1.month];
[components3 setDay:components1.day];
[components3 setHour:components2.hour];
[components3 setMinute:components2.minute];
// Generate a new NSDate from components3.
NSDate *combinedDate = [gregorianCalendar dateFromComponents:components3];
// combinedDate contains both your date and time!
NSString *lastdate =[formatter3 stringFromDate:combinedDate];
NSLog(@"combinedDate%@",lastdate);
[delegate.notificationBirthDateArray removeObjectAtIndex:i];
[delegate.notificationBirthDateArray insertObject:lastdate atIndex:i];
}
NSLog(@"new%@",delegate.notificationBirthDateArray);
}