2010-05-21 65 views
3

我想创建一个函数,导致下周五的日期,但我没有计划如何去做。有没有人给我一个好的提示?iPhone NSDate例如。下周五

+1

[将组件添加到日期](http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836- SW3)部分的日期和时间编程指南将很有用。清单2的代码可以查找当周的星期天。修改它,以找到下周五。 – DyingCactus 2010-05-22 03:28:33

回答

2

E.g.使用NSDate获取当前日期,然后使用NSCalendar的'components> fromDate:'获取NSDateComponents,然后将时间差添加到下一个星期五,并创建一个新的NSDate,Bob's是您的叔叔。

+0

我会试着找出那条路线。欢呼声 – 2010-05-22 01:10:26

-2

这是我的解决方案,只是为了提醒你,星期六是星期五展示之前。 欢呼所有

NSDate *today = [[NSDate alloc] init]; 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today]; 
int weekday = [weekdayComponents weekday]; 


int iOffsetToFryday = -weekday + 6; 
weekdayComponents.weekday = iOffsetToFryday; 

NSDate *nextFriday = [[NSCalendar currentCalendar] dateByAddingComponents:weekdayComponents toDate:today options:0]; 
+2

如果在星期六运行时返回错误的答案,您怎么认为这是正确的? – 2010-05-24 20:18:14

+0

我看到这不是正确的方式来处理 – 2010-06-20 23:55:47

2

这里是获取未来5日在公历我工作的解决方案:

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

这应该工作

+ (NSDate *) dateForNextWeekday: (NSInteger)weekday { 

NSDate *today = [[NSDate alloc] init]; 
NSCalendar *gregorian = [[NSCalendar alloc] 
         initWithCalendarIdentifier:NSGregorianCalendar]; 

// Get the weekday component of the current date 
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit 
                fromDate:today]; 

/* 
Add components to get to the weekday we want 
*/ 
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init]; 
NSInteger dif = weekday-weekdayComponents.weekday; 
if (dif<=0) dif += 7; 
[componentsToSubtract setDay:dif]; 

NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract 
                toDate:today options:0]; 

return beginningOfWeek; 

}

+0

根据问题的要求,这种情况下实际存在一个错误。如果您正在尝试获取当前工作日的下一个实例(即星期五的下周五),则最终会提供当前日期。解决的办法是将'dif <0'改为'dif <= 0'。 – Dima 2013-05-17 17:43:18

0

保持它简单,安全,可读! (.... KISSAR?)

#define FRIDAY 6 

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
dayComponent.day = 1; 

NSDate *nextFriday = [NSDate date]; 
NSInteger iWeekday = [[gregorian components:NSWeekdayCalendarUnit fromDate:nextFriday] weekday]; 

while (iWeekday != FRIDAY) { 
    nextFriday = [gregorian dateByAddingComponents:dayComponent toDate:nextFriday options:0]; 
    iWeekday = [[gregorian components:NSWeekdayCalendarUnit fromDate:nextFriday] weekday]; 
} 

现在nextFriday有你的约会。

希望这会有所帮助!

编辑 请注意,如果当前日期已经是星期五,它将返回,而不是下一个星期五。如果这是不可取的,只需在一天之后启动nextFriday(所以如果当前日期是星期五,它将在星期六开始,迫使下一个星期五。如果当前日期是星期四,您将自动在下个星期五不需要循环)。