问题
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
回答1:
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];
回答2:
This is crude but would work:
NSDate *prevWeekDate = [currentDate dateByAddingTimeInterval:(-60 * 60 * 24 * 7)];
Click for detail on dateByAddingTimeInterval:
来源:https://stackoverflow.com/questions/6158299/how-to-get-the-date-a-week-in-the-past-from-today