How to add a number of weeks to an NSDate?

前端 未结 4 1656
一生所求
一生所求 2021-01-18 03:31

I have in the past used the below function to add on a specific time interval using NSDateComponents to an existing date.

(NSDate *)dateByAddin         


        
4条回答
  •  耶瑟儿~
    2021-01-18 03:58

    You can add a category on NSDate with the following method:

    - (NSDate *) addWeeks:(NSInteger)weeks
    {
        NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *components=[[NSDateComponents alloc] init];
        components.day = weeks * 7;
    
        return [gregorian dateByAddingComponents:components toDate:self options:0];
    }
    

提交回复
热议问题