【问题标题】:CoreData - How to set date and time independent of each other?CoreData - 如何设置彼此独立的日期和时间?
【发布时间】:2010-09-06 18:21:52
【问题描述】:
我有一个名为 visitDate 的 coredata 属性
我想使用单独的选择器相互独立地设置日期和时间。
我意识到在设置时间时,我需要获取visitDate的现有日期、月份和年份,但只能从NSDatePicker中设置时间。
反之,我在设置日期的时候,需要抓取visitDate现有的时分秒,只设置日期。
我想我要问的是如何通过使用另一个 NSDate 实例的选择元素来创建一个 NSDate 实例?
【问题讨论】:
标签:
iphone
nsdate
nsdateformatter
【解决方案2】:
可能比必要的代码多一点,但这就是我的工作。
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);
}
【解决方案3】:
如果你使用的是 UIDatePicker(你提到了 NSDatePicker),你可以设置 UIDatePicker.datePickerMode 和 UIDatePicker.date。如果两个选择器同时出现在屏幕上,您可以在 UIControlEventValueChanged 事件上复制日期。如果没有,在显示选择器时设置 UIDatePicker.date 并在隐藏选择器时将其复制到 visitDate 可能会更容易。