Reputation: 239
I am trying to add hour and minutes to array of dates following is my code and i have put NSLog of old array and after array
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"
)
Issue is time which is obtained gets added in correct way which you see above, but where is my dates gone? they should be there too.
Please help me with this.
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);
}
Upvotes: 0
Views: 182
Reputation: 47699
To add an hours/minutes time interval to an NSDate, use dateWithTimeInterval:sinceDate:
.
NSDate* oldDate = <some date>;
NSTimeInterval timeToAdd = ((hoursToAdd * 60) + minutesToAdd) * 60.0;
NSDate* newDate = [theDate dateWithTimeInterval:timeToAdd sinceDate:oldDate];
You could also do it with NSDateComponents, or by formatting the date, manipulating the characters, and scanning it back to NSDate, but both take dozens of lines and are fraught with chances for error.
(However, if you are starting with a character date, and you just want to add a "boiler plate" time value, just concatenate the pieces together and scan the result:
NSString* oldDate = @"22/06/14";
NSString* dateWithTime = [oldDate appendString:@" 6:00:00 PM GMT+05:30"];
NSDateFormatter* df = [NSDateFormatter new];
df.dateFormat = @"dd/MM/yy h:mm:ss A 'GMT'ZZZZZ"; (or something like that)
NSDate* scannedDate = [df dateFromString:dateWithTime];
(Or, if you don't need the NSDate, skip the date formatter entirely.)
Upvotes: 0
Reputation: 1827
I see one issue here :
for (int i=0; i<[delegate.notificationBirthDateArray count]; i++) {
//Your code
[delegate.notificationBirthDateArray removeObjectAtIndex:i];
[delegate.notificationBirthDateArray insertObject:lastdate atIndex:i];
}
You shouldn't use the notificationBirthDateArray
in the for loop and do actions on it , it will bring unexpected problems,so i recommend on using another array as a placeholder in the for loop.
NSArray*arr = [NSArray arrayWithArray:delegate.notificationBirthDateArray];
then if you want do this:
for (int i=0; i<arr.count; i++)
{
//Your code
[delegate.notificationBirthDateArray removeObjectAtIndex:i];
[delegate.notificationBirthDateArray insertObject:lastdate atIndex:i];
}
Could be more things here, but i think you should modify like i mentioned and post the new outcome.
Remove from the formatter1 the line :
[formatter1 setTimeStyle:NSDateFormatterLongStyle];
and modify the line :
[formatter1 setDateFormat:@"dd:MM:yyyy"];
to :
[formatter1 setDateFormat:@"dd/MM/yyyy"];
If your string dates are exactly like in the old array;
Upvotes: 1