Diffy
Diffy

Reputation: 735

format uidatepicker to another nsdate

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"

Upvotes: 0

Views: 694

Answers (1)

lnafziger
lnafziger

Reputation: 25740

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 NSDate calendar system.

You either need to store the NSDate that 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:

NSCalendar          *cal            = [NSCalendar currentCalendar];

NSDate              *pickerDate     = self.datePickerView.date;
NSDateComponents    *pickerComps    = [cal components:  NSWeekdayCalendarUnit |
                                                        NSHourCalendarUnit |
                                                        NSMinuteCalendarUnit |
                                                        NSSecondCalendarUnit
                                             fromDate:pickerDate];

NSDate              *currentDate    = [NSDate date];
NSDateComponents    *currentComps   = [cal components:  NSYearCalendarUnit | 
                                                        NSMonthCalendarUnit | 
                                                        NSWeekdayCalendarUnit |
                                                        NSDayCalendarUnit |
                                                        NSHourCalendarUnit |
                                                        NSMinuteCalendarUnit |
                                                        NSSecondCalendarUnit
                                             fromDate:currentDate];

// Start with the current date and add/subtract the number of days needed to make it the same day of the week
NSDateComponents    *newComps       = [currentComps copy];
NSUInteger          weekdayDiff     = pickerComps.weekday - currentComps.weekday;
newComps.day                        = newComps.day + weekdayDiff;
// If needed, add 7 days in order to move this date into the future
if (newComps.day < currentComps.day) {
    newComps.day = newComps.day + 7;
}

NSDate *newDate = [cal dateFromComponents:newComps];
if ([newDate compare:currentDate] == NSOrderedAscending) {
    // This is true when the weekday started out the same but the time of day is earlier in the picker.  
    // Add 7 days
    newComps.day = newComps.day + 7;
    newDate = [cal dateFromComponents:newComps];
}

NSLog(@"%@", newDate);

Upvotes: 1

Related Questions