Modifying NSDate to represent 1 month from today

后端 未结 5 459
夕颜
夕颜 2021-01-31 11:07

I\'m adding repeating events to a Cocoa app I\'m working on. I have repeat every day and week fine because I can define these mathematically (3600*24*7 = 1 week). I use the foll

5条回答
  •  一向
    一向 (楼主)
    2021-01-31 12:00

    You can do like the below:

    NSDate *today = [NSDate date];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.month = 1;
    NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:today options:0];
    [components release];
    
    NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nextMonth];
    NSDate *nextMonthDay = [gregorian dateFromComponents:nextMonthComponents];
    
    [gregorian release]; 
    

提交回复
热议问题