iPhone NSDate例如。下周五 [英] iPhone NSDate eg. next Friday

查看:75
本文介绍了iPhone NSDate例如。下周五的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个导致下周五日期的功能,但我没有计划如何去做。有没有人给我一个好的暗示?

I want to create a function which results the date of next Friday but I have no plan how to do it. Has anyone a good hint to me ?

推荐答案

这是我在格里高利历中接下来的5个星期日的工作解决方案:

Here is my working solution for getting the next 5 Sundays in Gregorian calendar:

self.nextBeginDates = [NSMutableArray array];

NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int currentWeekday = [weekdayComponents weekday]; //[1;7] ... 1 is sunday, 7 is saturday in gregorian calendar

NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setDay:8 - currentWeekday];   // add some days so it will become sunday

// build weeks array
int weeksCount = 5;
for (int i = 0; i < weeksCount; i++) {
    [comp setWeek:i];   // add weeks

    [nextBeginDates addObject:[[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:[NSDate date] options:0]];
}
[comp release];

这篇关于iPhone NSDate例如。下周五的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆