2010-05-21 51 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(所以如果當前日期是星期五,它將在星期六開始,迫使下一個星期五。如果當前日期是星期四,您將自動在下個星期五不需要循環)。