簡體   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