CoreData - 如何设置彼此独立的日期和时间?

发布于 2024-09-18 03:17:38 字数 250 浏览 12 评论 0原文

我有一个名为 VisitDate 的 coredata 属性,

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


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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怕倦 2024-09-25 03:17:38

可能代码比必要的多一点,但这就是我正在工作的。

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

}

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

}
春夜浅 2024-09-25 03:17:38

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文