I have a UIDatePicker and I get the date using this:
NSDate *pickerDate = [datePickerView date];
I want to create a date in the future which is based on the day of the week and the time (HH:mm:ss) from the pickerDate. Here is the code that I am using but the date that it generates is wrong:
UPDATED CODE
//format the uidatepicker
NSDate *dateSet = pickerDate;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:@"EEEE HH:mm:ss"];
NSString *dateSetString = [df stringFromDate:dateSet];
NSLog(@"repeatDateString %@",dateSetString);//prints "Tuesday 12:12:43"
//create a new nsdate using the new format
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat2 setDateFormat: @"EEEE HH:mm:ss"];
NSDate *newDate = [[NSDate alloc] init];
newDate = [dateFormat2 dateFromString:dateSetString];
NSLog(@"new Date %@",newDate);//prints "1970-01-06 04:42:43 +0000"
The problem is that your date string does not contain all of the information from the picker, and does not contain enough information in order to re-create the date. It is missing the Day, Month, and Year so when you re-create it it assumes that you want to start at the start of iOS’s
NSDatecalendar system.You either need to store the
NSDatethat you receive from the picker (preferably), or you need to use a date format that contains enough information to create a specific date and time.EDIT
Based on your comment, you want to just use the weekday and time from the datePicker and determine the next date in the future with those values. Here is code that will accomplish that: