2013-06-28 91 views
1

我想獲取當前的&下週星期日期的名稱。在IOS SDK中獲取星期日期

即,如果今天的日期是28-06-2013(星期五),那麼我想從本週的23-06-2013(星期日)到29-06-2013(星期六)的日期爲

下週 我想從30-06-2013(星期日)獲得日期2013年6月7日(星期六)。

我該怎麼做?

謝謝

+0

你想在UIDatepicker這有限的日期或者你只是想要任何其他功能的日期? – wesley

+0

@wesley,我沒有使用datePicker。 – user2526811

回答

12

下面是代碼示例。

-(NSArray*)daysThisWeek 
{ 
    return [self daysInWeek:0 fromDate:[NSDate date]]; 
} 

-(NSArray*)daysNextWeek 
{ 
    return [self daysInWeek:1 fromDate:[NSDate date]]; 
} 
-(NSArray*)daysInWeek:(int)weekOffset fromDate:(NSDate*)date 
{ 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    //ask for current week 
    NSDateComponents *comps = [[NSDateComponents alloc] init]; 
    comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:date]; 
    //create date on week start 
    NSDate* weekstart=[calendar dateFromComponents:comps]; 
    if (weekOffset>0) { 
     NSDateComponents* moveWeeks=[[NSDateComponents alloc] init]; 
     moveWeeks.week=1; 
     weekstart=[calendar dateByAddingComponents:moveWeeks toDate:weekstart options:0]; 
    } 

    //add 7 days 
    NSMutableArray* week=[NSMutableArray arrayWithCapacity:7]; 
    for (int i=1; i<=7; i++) { 
     NSDateComponents *compsToAdd = [[NSDateComponents alloc] init]; 
     compsToAdd.day=i; 
     NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0]; 
     [week addObject:nextDate]; 

    } 
    return [NSArray arrayWithArray:week]; 
} 
+0

感謝您的回覆....太棒了!有用。但是我怎樣才能得到下一週的日期呢? – user2526811

+1

查看編輯的answear –

+0

感謝您的回覆..... – user2526811

0

使用NSDateComponents找出一週的某一天,然後找到之前和之後所需要的日期。

+0

感謝您的回覆.....但你能發表一些代碼嗎? – user2526811

+0

檢查@HeftyHijacker答案中的鏈接以獲取適當的代碼。 – Wain

1

我打算複製並粘貼一個答案,但我建議去閱讀這篇文章:Current Week Start and End Date。它看起來會幫助你實現你想要的。

像這樣的東西可能:

NSDate *now = [NSDate date]; 

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSInteger weekNumber = [[calendar components: NSWeekCalendarUnit fromDate:now] week]; 

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now]; 
[comp setWeek:weekNumber]; //Week number. 

int i; 

for (i = 1; i < 8; i=i+1){ 

    [comp setWeekday:i]; //First day of the week. Change it to 7 to get the last date of the week 

    NSDate *resultDate = [gregorian dateFromComponents:comp]; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"EEEE MMMM d, yyyy"]; 

    NSString *myDateString = [formatter stringFromDate:resultDate]; 
    NSLog(@"%@",myDateString); 

} 
+0

感謝您的回覆....但我希望所有7天不僅開始和結束日期。 – user2526811

+0

一旦你有開始和結束日期,你可以迭代並填寫空白。 –

+0

已更新我的答案以包含解決方案。這給你一週中的所有7個日期。 –

0

嗯,我不是這個或任何方面的專家,而是一個愚蠢的方式做,這是第一個能夠從NSDate識別星期幾。然後,您可以添加if語句,然後if語句可以添加和減去日期和nslogprintf每個人。或者我認爲通過訪問ios日曆有一種方法。

沒關係,所以上面說得很多,做得比我說的還多。是的,去檢查我之前的帖子上的鏈接。

0

使用NSCalendar實例將您的開始NSDate實例轉換爲NSDateComponents實例,將1天添加到NSDateComponents並使用NSCalendar將其轉換回NSDate實例。重複最後兩個步驟,直到到達結束日期。

NSDate *currentDate = [NSDate date]; 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *comps = [[NSDateComponents alloc] init]; 
[comps setDay:1]; 
NSDate *nextDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0]; 

或者找到開始日期和結束日期。像

NSDate *startDate = [NSDate date]; 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *comps = [[NSDateComponents alloc] init]; 
[comps setDay:7]; 
NSDate *endDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0]; 

然後, NSDate nextDate;

for (nextDate = startDate ; [nextDate compare:endDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60]) { 
    // use date 
} 
+0

你的代碼在當前日期(從現在開始的下一個7天)後的7天內得到,這不是OP要求的 –