畏縮
它看起來像你有兩個不同的datePickers? datePicker
和datePicker1
?那是怎麼回事?
而且,這不會做你期待什麼:
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*364];
這是創建在未來究竟31449600秒一個新的日期。這是而不是做其他事情。
你想要做的是從當前日期提取所有日期的組件和零出來:
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];
見http://stackoverflow.com/questions/5358678/how-do- i-set-an-existing-nsdates-time – Glenn 2012-04-10 17:08:20