【问题标题】:Set the time of the DatePicker设置 DatePicker 的时间
【发布时间】:2012-04-10 17:05:04
【问题描述】:

如何将 DatePicker 的时间设置为当前日期的 00:00:00?

   - (void) awakeFromNib
    {
        [datePicker setDateValue:[NSDate date]];
        NSDate *now = [NSDate date];
        int daysToAdd = 364;
        NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
        [datePicker1 setDateValue:newDate1];
    }

【问题讨论】:

标签: xcode datepicker nsdate nscalendar


【解决方案1】:

畏缩

看起来您有两个不同的日期选择器? datePickerdatePicker1?这是怎么回事?

此外,这并没有达到您的预期:

NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*364];

这将创建一个新的日期,该日期正好是未来 31,449,600 秒。它没有做除此之外的任何事情。

您要做的是从当前日期中提取所有日期组件并将它们归零:

NSDate *now = [NSDate date];
NSDateComponents *nowComponents = [[NSCalendar currentCalendar] components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:now];

// technically these next lines are unnecessary, since we only pulled out the era-year-month-day, but they're included here for understanding/completeness:

[nowComponents setHour:0];
[nowComponents setMinute:0];
[nowComponents setSecond:0];

// now we can turn it back into a date:

NSDate *todayAtMidnight = [[NSCalendar currentCalendar] dateFromComponents:nowComponents];

【讨论】:

    猜你喜欢
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多