Make a new NSDate by combining two NSDates

前端 未结 5 1752
故里飘歌
故里飘歌 2021-02-15 11:11

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

相关标签:
5条回答
  • 2021-02-15 11:36

    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 the NSCalendar.

    Use the NSCalendar methods

    – components:fromDate:
    – dateFromComponents:
    

    and the corresponding unit flags

    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit; 
    
    0 讨论(0)
  • 2021-02-15 11:39

    Update Swift 4 & 4.1

     func combineDateAndTimeToOneDateForAPI() {
    
        let datefrmter = DateFormatter()
        datefrmter.dateFormat = "dd/MM/yyyy"
        let dateFromString = datefrmter.date(from: lblDate.text!)
    
        datefrmter.dateFormat = "h:mm a"
        let timeFromString = datefrmter.date(from: lblTime.text!)
    
        let calendar = NSCalendar.current
    
        let componentsOne = calendar.dateComponents([.year,.month,.day], from: dateFromString!)
        let componentsTwo = calendar.dateComponents([.hour,.minute,.second,.nanosecond], from: timeFromString!)
    
        let totalComponents = NSDateComponents()
        totalComponents.year = componentsOne.year!
        totalComponents.month = componentsOne.month!
        totalComponents.day = componentsOne.day!
        totalComponents.hour = componentsTwo.hour!
        totalComponents.minute = componentsTwo.minute!
        totalComponents.second = componentsTwo.second!
        totalComponents.nanosecond = componentsTwo.nanosecond!
    
        let finalDate = calendar.date(from: totalComponents as DateComponents)
        print(finalDate)
    
    }
    
    0 讨论(0)
  • 2021-02-15 11:46

    It's not as compact as darvids0n's, 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];
    
    0 讨论(0)
  • 2021-02-15 11:46

    For the records, 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 like this

    NSDate *day3 = 
        [day2 dateByTakingComponents: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                            fromDate: day1];
    
    0 讨论(0)
  • 2021-02-15 11:55
    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:

    1. NSDateComponents Class Reference
    2. NSCalendar Class Reference
    0 讨论(0)
提交回复
热议问题