CoreData - 如何相互独立地设置日期和时间?

时间:2010-09-06 18:21:53

标签: iphone nsdate nsdateformatter

我有一个名为visitDate的coredata属性

我想使用单独的选择器相互独立地设置日期和时间。


我意识到当我设置时间时,我需要获取visitDate的现有日期,月份和年份,但只能从NSDatePicker设置时间。

相反,当我设置日期时,我需要抓住visitDate的现有小时,分钟和秒,并且只设置日期。

我想我要问的是如何通过使用另一个NSDate实例的select元素来创建NSDate实例?

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

可能需要一些不必要的代码,但这就是我的工作。

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);

}

答案 2 :(得分:0)

如果您正在使用UIDatePicker(您提到过NSDatePicker),则可以设置UIDatePicker.datePickerMode和UIDatePicker.date。如果两个选择器同时显示在屏幕上,您可以在UIControlEventValueChanged事件上复制日期。如果没有,当您显示选择器时可能更容易设置UIDatePicker.date,并在隐藏选择器时将其复制到visitDate。