Subtract 7 days from current date

前端 未结 11 1084
灰色年华
灰色年华 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:27

    code:

    NSDate *currentDate = [NSDate date];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setDay:-7];
    NSDate *sevenDaysAgo = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
    NSLog(@"\ncurrentDate: %@\nseven days ago: %@", currentDate, sevenDaysAgo);
    [dateComponents release];
    

    output:

    currentDate: 2012-04-22 12:53:45 +0000
    seven days ago: 2012-04-15 12:53:45 +0000
    

    And I'm fully agree with JeremyP.

    BR.
    Eugene

提交回复
热议问题