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

点点圈 提交于 2020-01-24 12:52:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!