Strong Like Bull
Strong Like Bull

Reputation: 11297

How to get the date a week in the past from today?

I know how to get the date in iOS. I need to find last weeks date. So for example if today is 05/27/2011, I need to be able to get 05/20/2011

Thanks

Upvotes: 2

Views: 1173

Answers (2)

Desdenova
Desdenova

Reputation: 5378

How about:

    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];

Upvotes: 5

dredful
dredful

Reputation: 4388

This is crude but would work:

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

Click for detail on dateByAddingTimeInterval:

Upvotes: 6

Related Questions