简体   繁体   中英

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

This is crude but would work:

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

Click for detail on dateByAddingTimeInterval:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM