繁体   English   中英

如何从今天获取过去一周的日期?

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

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

谢谢

这是粗略的,但会起作用:

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

单击以获取有关dateByAddingTimeInterval 的详细信息:

怎么样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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