2011-11-17 98 views
1

我想知道當天 - 比做一些特別的東西在這一天開關「星期幾」

週一=做到這一點
週日=做到這一點等等

我知道如何做這個開關,但我怎麼能問到當前?

if (...) { 
do something; 
} 
else { 
switch (..) { 

case 0: 
currentPageNumber = 2; 
break; 

case 1: 
currentPageNumber = 3; 

<...> 
default: 
break; 
} 

我認爲switch/case是做我想要的正確方法。感謝您幫助我;-)

+0

可能重複[如何檢查星期幾是星期幾週二,週五?)和比較兩個NSDates?](http://stackoverflow.com/questions/583692/how-to-check-what-day-of-the-week-it-is-ie-tues-fri- and-compare-two-nsdate) – trojanfoe

回答

1
[NSDate date] 

使你在你的時區當前的日期

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]] 

給你一個NSDateComponents實例。你可以用

components.weekday 
+0

YEPP - wunderful THANKS ALOT。這工作正常;-) – webschnecke

0

使用下面的代碼獲得工作日(從1-7/SU-SA值)...

NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:DateFrom]; 

這將要給你NSDateComponents.you的實例可以得到平日使用..

int weekday1 = [weekdayComponents weekday]; 

後使用烏爾logic..as

if (weekday1 == 1) 
    { 
    //do something 
    } 
. 
. 

注意1意味着週日等..