views:

60

answers:

3

I have a coredata attribute called visitDate

I want to set the date and time independently of each other using separate pickers.


I realize that when I set the time, I need to grab the existing date, month and year of visitDate, but only set the time from the NSDatePicker.

Conversely, when I set the date, I need to grab the existing hour, minute and second of visitDate, and only set the date.

I guess what I'm asking is how does one create an NSDate instance by using select elements of another NSDate instance?

+1  A: 

Using NSDateComponents. See also the Date and Time Programming Guide.

Rob Napier
A: 

If you're using UIDatePicker (you mentioned NSDatePicker), you can set UIDatePicker.datePickerMode and UIDatePicker.date. If both pickers will be on-screen at the same time, you can copy the date over on a UIControlEventValueChanged event. If not, it might be easier to set UIDatePicker.date when you show the picker and copy it to visitDate when you hide the picker.

tc.
A: 

Might be a little more code than necessary, but this is what I got working.

if (editing == Date) {

    if ([visit valueForKey:@"visitDate"] == nil) {
        [visit setValue:datePicker.date forKey:@"visitDate"];
    }
    else {
        NSDateFormatter *hourFormat = [[NSDateFormatter alloc] init];
        [hourFormat setDateFormat:@"HH"];
        NSString *hourString = [hourFormat stringFromDate:[visit valueForKey:@"visitDate"]];

        NSDateFormatter *minuteFormat = [[NSDateFormatter alloc] init];
        [minuteFormat setDateFormat:@"mm"];
        NSString *minuteString = [minuteFormat stringFromDate:[visit valueForKey:@"visitDate"]];

        NSDateFormatter *dayFormat = [[NSDateFormatter alloc] init];
        [dayFormat setDateFormat:@"dd"];
        NSString *dayString = [dayFormat stringFromDate:datePicker.date];

        NSDateFormatter *monthFormat = [[NSDateFormatter alloc] init];
        [monthFormat setDateFormat:@"MM"];
        NSString *monthString = [monthFormat stringFromDate:datePicker.date];

        NSDateFormatter *yearFormat = [[NSDateFormatter alloc] init];
        [yearFormat setDateFormat:@"YYYY"];
        NSString *yearString = [yearFormat stringFromDate:datePicker.date];

        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setMinute:[minuteString intValue]];
        [components setHour:[hourString intValue]];
        [components setDay:[dayString intValue]]; //Set Day
        [components setMonth:[monthString intValue]]; //Set Month
        [components setYear:[yearString intValue]]; //Set Year
        NSCalendar *gregorian = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *date = [gregorian dateFromComponents:components];

        [visit setValue:date forKey:@"visitDate"];
    }
    [visitTableView reloadData];
    NSLog(@"Date Saved as %@", visit.visitDate);

}
else if (editing == Time) {

    if ([visit valueForKey:@"visitDate"] == nil) {
        [visit setValue:datePicker.date forKey:@"visitDate"];
    }
    else {
        NSDateFormatter *hourFormat = [[NSDateFormatter alloc] init];
        [hourFormat setDateFormat:@"HH"];
        NSString *hourString = [hourFormat stringFromDate:datePicker.date];

        NSDateFormatter *minuteFormat = [[NSDateFormatter alloc] init];
        [minuteFormat setDateFormat:@"mm"];
        NSString *minuteString = [minuteFormat stringFromDate:datePicker.date];

        NSDateFormatter *dayFormat = [[NSDateFormatter alloc] init];
        [dayFormat setDateFormat:@"dd"];
        NSString *dayString = [dayFormat stringFromDate:[visit valueForKey:@"visitDate"]];

        NSDateFormatter *monthFormat = [[NSDateFormatter alloc] init];
        [monthFormat setDateFormat:@"MM"];
        NSString *monthString = [monthFormat stringFromDate:[visit valueForKey:@"visitDate"]];

        NSDateFormatter *yearFormat = [[NSDateFormatter alloc] init];
        [yearFormat setDateFormat:@"YYYY"];
        NSString *yearString = [yearFormat stringFromDate:[visit valueForKey:@"visitDate"]];

        NSDateComponents *components = [[NSDateComponents alloc] init];

        [components setMinute:[minuteString intValue]];
        [components setHour:[hourString intValue]];
        [components setDay:[dayString intValue]]; //Set Day
        [components setMonth:[monthString intValue]]; //Set Month
        [components setYear:[yearString intValue]]; //Set Year
        NSCalendar *gregorian = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *date = [gregorian dateFromComponents:components];

        [visit setValue:date forKey:@"visitDate"];
    }
    [visitTableView reloadData];
    NSLog(@"Date Saved as %@", visit.visitDate);

}
monotreme