0

如果我说upcoming monday那我怎么能得到日期upcoming monday

就像,今天Thu, 04-04-2013比我怎么知道即将到来的日期wednesday

希望我能够澄清你。

这里问了类似的问题,但我无法理解:

在Objective-C中获取下周六的日期?

4

2 回答 2

6
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [NSDate date];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
NSInteger todayWeekday = [weekdayComponents weekday];

enum Weeks {
    SUNDAY = 1,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};

NSInteger moveDays=WEDNESDAY-todayWeekday;
if (moveDays<=0) {
    moveDays+=7;
}

NSDateComponents *components = [NSDateComponents new];
components.day=moveDays;

NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate* newDate = [calendar dateByAddingComponents:components toDate:date options:0];

NSLog(@"%@",newDate);
于 2013-04-04T13:17:26.890 回答
1

只是为了激怒Anoop;):

NSDate* now = [NSDate date];
NSTimeInterval nowInterval = [now timeIntervalSince1970];
int days = (int)(nowInterval / (24 * 60 * 60));
int today = (days + 4) % 7;
NSDate* saturday = [NSDate dateWithTimeIntervalSince1970:nowInterval + (6 - today) * 24 * 60 * 60];
NSLog(@"saturday = %@", saturday);
于 2013-04-04T18:43:44.293 回答