2016-06-17 46 views
1

我想獲取日期,該日期是兩個日期之間的月份的第三個星期六。我做了,但不是我想要的。獲取開始日期和結束日期之間的月份的第3個星期六

NSInteger count = 0; 
NSInteger saturday = 7; 

// Set the incremental interval for each interaction. 
NSDateComponents *oneDay = [[NSDateComponents alloc] init]; 
[oneDay setDay:1]; 

// Using a Gregorian calendar. 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"]; 
     NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"]; 


// Iterate from fromDate until toDate 
while ([currentDate compare:toDate] == NSOrderedAscending) { 

NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate]; 
[dateComponents setWeekdayOrdinal:3]; 

if (dateComponents.weekday == saturday) { 
    count++; 
    NSLog(@"Date = %@",currentDate); 
} 

// "Increment" currentDate by one day. 
currentDate = [calendar dateByAddingComponents:oneDay 
                toDate:currentDate 
                options:0]; 
} 

在我的代碼我已經成功地得到星期六但它的所有星期六得到這在startdate & enddate之間。 我也試試這個:

[dateComponents setWeekdayOrdinal:3]; 但無法正常工作。

這是我的日誌

2016-06-17 11:29:18.319 Floacstation[1715:84848] Current Date :2016-06-16 18:30:00 +0000 
2016-06-17 11:29:18.319 Floacstation[1715:84848] To Date : 2016-08-16 18:30:00 +0000 

2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-06-19 18:30:00 +0000 
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-06-26 18:30:00 +0000 
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-03 18:30:00 +0000 
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-10 18:30:00 +0000 
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-17 18:30:00 +0000 
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-24 18:30:00 +0000 
2016-06-17 11:29:18.320 Floacstation[1715:84848] Saturday Date = 2016-07-31 18:30:00 +0000 
2016-06-17 11:29:18.321 Floacstation[1715:84848] Saturday Date = 2016-08-07 18:30:00 +0000 
2016-06-17 11:29:18.321 Floacstation[1715:84848] Saturday Date = 2016-08-14 18:30:00 +0000 
+0

發表您的日誌.. – Simmy

+0

@Sneha我編輯我的職務原木 –

+0

打印的currentdate和TODATE到的NSLog和展示它太.. – Simmy

回答

3

原因:

注意,您所輸入的日期17th但其返回你16th。至於它正在考慮UTC格式,只要你是printing NSDate in NSLogconverting it explicitly to NSString

,只要你想在字符串中的日期,然後使用NSDateFormatter正如我在本準則已使用..

你所面對的問題是,僅僅因爲這一點。 所以,無論你再次&檢查使用的日期,只是replace the method of converting date到輸出的字符串..

試試這個......必須給你正確的答案...

NSInteger count = 0; 
NSInteger saturday = 7; 

// Set the incremental interval for each interaction. 
NSDateComponents *oneDay = [[NSDateComponents alloc] init]; 
[oneDay setDay:1]; 

// Using a Gregorian calendar. 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"]; 
     NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"]; 


// Iterate from fromDate until toDate 
while ([currentDate compare:toDate] == NSOrderedAscending) { 

NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate]; 
[dateComponents setWeekdayOrdinal:3]; 

if (dateComponents.weekday == saturday) { 
    count++; 

    if (count==3) { 
      NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; 
      [email protected]"dd MM YYYY"; 

      NSLog(@"Date = %@",[formatter stringFromDate:currentDate]); 
     } 
    } 

} 

//有沒有需要增加這個日期..

// "Increment" currentDate by one day. 
// currentDate = [calendar dateByAddingComponents:oneDay 
//             toDate:currentDate 
//             options:0]; 

希望這有助於...:)

+0

對數值 當前日期:2016-06-16 18:30:00 +0000 迄今:2016-08-16 18:30:00 +0000 日期= 04 07 2016 而在我的日曆顯示星期一 –

0
NSInteger count = 0; 
NSInteger saturday = 7; 

// Set the incremental interval for each interaction. 
NSDateComponents *oneDay = [[NSDateComponents alloc] init]; 
[oneDay setDay:1]; 

// Using a Gregorian calendar. 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"]; 
     NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"]; 


// Iterate from fromDate until toDate 
while ([currentDate compare:toDate] == NSOrderedAscending) { 

NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate]; 
[dateComponents setWeekdayOrdinal:3]; 

if (dateComponents.weekday == saturday) { 
    count++; 
    NSLog(@"3rd Satureday Date = %@",currentDate); 
    if(count == 3) 
     break; 
} 

// "Increment" currentDate by one day. 
currentDate = [calendar dateByAddingComponents:oneDay 
                toDate:currentDate 
                options:0]; 
} 

這是我的日誌

2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-06-17 18:30:00 +0000 
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-06-24 18:30:00 +0000 
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-01 18:30:00 +0000 
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-08 18:30:00 +0000 
2016-06-17 11:07:53.335 Floacstation[1568:75953] Date = 2016-07-15 18:30:00 +0000 
2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-07-22 18:30:00 +0000 
2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-07-29 18:30:00 +0000 
2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-08-05 18:30:00 +0000 
2016-06-17 11:07:53.336 Floacstation[1568:75953] Date = 2016-08-12 18:30:00 +0000 
+0

它不能正常工作 –

+0

nslog的價值是什麼? –

+0

只得到星期六的一個日期,並且那個價值我得到它錯了它不是星期六日期 –

1

NSCalendar擁有強大的技能,並且不用重複循環

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
NSDate *currentDate = [[UtilityClass sharedObject] stringToDate:@"2016-06-17" withFormate:@"yyyy-MM-dd"]; 
NSDate *toDate = [[UtilityClass sharedObject] stringToDate:@"2016-08-17" withFormate:@"yyyy-MM-dd"]; 

NSDateComponents *components = [[NSDateComponents alloc] init]; 
components.weekday = 7; 
components.weekdayOrdinal = 3; 
components.month = [calendar component:NSCalendarUnitMonth fromDate:currentDate]; 

NSDate *nextThirdSaturday = [calendar nextDateAfterDate:currentDate matchingComponents: components options: NSCalendarMatchNextTime]; 
if (nextThirdSaturday && [nextThirdSaturday compare:toDate] == NSOrderedAscending) { 
    NSLog(@"%@", nextThirdSaturday); 
} else { 
    NSLog(@"Not Found"); 
} 

編輯:

如果你想從一開始日期和開始和結束日期的使用範圍內計算出的第三個星期六這

NSDateComponents *components = [[NSDateComponents alloc] init]; 
components.weekday = 7; 

__block NSInteger count = 1; 
__block NSDate *found; 
[calendar enumerateDatesStartingAfterDate:currentDate matchingComponents:components options:NSCalendarMatchNextTime usingBlock:^(NSDate *date, BOOL exactMatch, BOOL *stop) { 
    if (count == 3) { found = date; } 
    if (found || [date compare:toDate] == NSOrderedDescending) *stop = YES; 
    count++; 
}]; 
NSLog(@"%@", found); 
+0

它不工作:( –

+0

它應該工作你會得到什麼結果 – vadian

+0

我只得到STARTDATE –

0

根據我的瞭解,你想要的日期是2016-07-02 ? 試試這個:

NSDate *currentDate = [NSDate dateWithString:@"2016-06-16" formatString:@"yyyy-MM-dd"]; 

NSDate *toDate = [NSDate dateWithString:@"2016-08-17" formatString:@"yyyy-MM-dd"]; 

NSLog(@"Oh, %@ is the 3rd Saturday", [[currentDate dateByAddingWeeks:2] dateByAddingDays:(saturday - currentDate.weekday + 1)]); 

我使用了一個名爲DateTools 3庫。

相關問題