我有NSDate* day1NSDate* day2。如何仅通过复制第一天的年/月/日和第二天的时/分/秒来制作NSDate* day3

最佳答案

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:day1];
NSDate *day3 = [[NSCalendar currentCalendar] dateFromComponents:comps];

unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:day2];
day3 = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:day3 options:0];

资料来源:
  • NSDateComponents Class Reference
  • NSCalendar Class Reference
  • 10-08 02:34