I am trying to combine the time value (hours, minutes, seconds) of one NSDate
with the date value (day, month, year) of a second NSDate
with this method:
+ (NSDate *)combineDate:(NSDate *)date withTime:(NSDate *)time {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit |
NSWeekdayCalendarUnit) fromDate:date];
NSDateComponents *timeComponents = [gregorian components:(NSDayCalendarUnit |
NSWeekdayCalendarUnit) fromDate:time];
NSDateComponents *comp = [NSDateComponents alloc];
[comp setSecond:[timeComponents second]];
[comp setHour:[timeComponents hour]];
[comp setMinute:[timeComponents minute]];
[comp setYear:[dateComponents year]];
[comp setMonth:[dateComponents month]];
[comp setDay:[dateComponents day]];
NSDate *combDate = [gregorian dateFromComponents:comp];
[gregorian release];
[comp release];
return combDate;
}
... but what it's returning is effectively gibberish (the year comes out as "1", for example). Anybody see what's wrong with this method?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…