Create a new NSDate by combining two NSDates

I have NSDate* day1 and NSDate* day2 . How can I make NSDate* day3 by copying only year / month / day from the first day and hour / minute / second from day2?

+6
objective-c cocoa nsdate macos
source share
4 answers
 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]; 

Sources:

+15
source share

It is not as compact as darvids0n, but it works.

 NSDate* day1 = [datePicker dateValue]; NSDate* day2 = [NSDate date]; NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:day1; NSInteger day = [components day]; NSInteger month = [components month]; NSInteger year = [components year]; components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit fromDate:day2]; NSInteger hour = [components hour]; NSInteger minute = [components minute]; NSInteger second = [components second]; NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease]; [comps setDay:day]; [comps setMonth:month]; [comps setYear:year]; [comps setHour:hour]; [comps setMinute:minute]; [comps setSecond:second]; NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *day3 = [cal dateFromComponents:comps]; 

or

 NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init]autorelease]; [tempFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString* message = [NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second]; NSDate *day3 = [tempFormatter dateFromString:message]; 
+1
source share

You need to create an NSCalendar, and then get the date components from the first and second dates, and then get the date using the new date components from NSCalendar.

Use NSCalendar Methods

 components:fromDate: – dateFromComponents: 

and corresponding flag units

 NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit; 
0
source share

For entries here is a more general approach

 @implementation NSDate (Additions) - (void)assignFlags:(NSUInteger)unitFlags to:(NSDateComponents *)components { NSDateComponents *myComponents = [[NSCalendar currentCalendar] components: unitFlags fromDate: self]; if (unitFlags & NSYearCalendarUnit) [components setYear: [myComponents year]]; if (unitFlags & NSMonthCalendarUnit) [components setMonth: [myComponents month]]; if (unitFlags & NSDayCalendarUnit) [components setDay: [myComponents day]]; if (unitFlags & NSHourCalendarUnit) [components setHour: [myComponents hour]]; if (unitFlags & NSMinuteCalendarUnit) [components setMinute: [myComponents minute]]; if (unitFlags & NSSecondCalendarUnit) [components setSecond: [myComponents second]]; } - (NSDate *)dateByTakingComponents:(NSUInteger)unitFlags fromDate:(NSDate *)date { NSUInteger allFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *allComponents = [[NSDateComponents alloc] init]; [date assignFlags: unitFlags to: allComponents]; [self assignFlags: allFlags & ~unitFlags to: allComponents]; NSDate *result = [[NSCalendar currentCalendar] dateFromComponents: allComponents]; [allComponents release]; return result; } @end 

You can call it as follows

 NSDate *day3 = [day2 dateByTakingComponents: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate: day1]; 
0
source share

All Articles