Subtract 7 days from current date

前端 未结 11 916
灰色年华
灰色年华 2021-01-29 22:41

It seems that I can\'t subtract 7 days from the current date. This is how i am doing it:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:N         


        
11条回答
  •  鱼传尺愫
    2021-01-29 23:15

    use dateByAddingTimeInterval method:

    NSDate *now = [NSDate date];
    NSDate *sevenDaysAgo = [now dateByAddingTimeInterval:-7*24*60*60];
    NSLog(@"7 days ago: %@", sevenDaysAgo);
    

    output:

    7 days ago: 2012-04-11 11:35:38 +0000
    

    Hope it helps

提交回复
热议问题