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];
prosseek
source share