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

时间:2011-05-27 22:32:02

标签: ios objective-c iphone nsdate

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

由于

2 个答案:

答案 0 :(得分:6)

这很粗糙但可行:

NSDate *prevWeekDate = [currentDate dateByAddingTimeInterval:(-60 * 60 * 24 * 7)];

点击查看dateByAddingTimeInterval:

的详细信息

答案 1 :(得分: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];