如何从今天开始一周的约会?

jin*_*ini 2 iphone objective-c nsdate ios

我知道如何在iOS中获取日期.我需要查找上周的日期.例如,如果今天是05/27/2011,我需要能够获得05/20/2011

谢谢

dre*_*ful 6

这很粗糙但可行:

NSDate *prevWeekDate = [currentDate dateByAddingTimeInterval:(-60 * 60 * 24 * 7)];
Run Code Online (Sandbox Code Playgroud)

单击dateByAddingTimeInterval上的详细信息:

  • 那里不应该有负面影响吗? (2认同)

Des*_*ova 5

怎么样:

    NSCalendar * calendar = [NSCalendar currentCalendar];

    NSDate * date = [NSDate date];

    NSDateComponents *components = [[NSDateComponents alloc] init];

    [components setWeek:-1];

    NSDate * lastweek = [calendar dateByAddingComponents:components toDate:date options:0];

    NSLog(@"%@", lastweek);

    [components release];