2014-01-09 46 views
0

假設我們使用的是公曆日曆系統之間的號碼週末,什麼是制定出有兩個NSDate對象之間發生這週末的數目最好的方法是什麼?2個NSDates

我目前的做法是計算兩個NSDate之間的天數,然後除以7和floor()結果。這給了我幾周的時間,結果是週末的數量。因爲如果一週尚未完成(並且該部門有剩餘部分),我們就會得到結果,它會忽略該結果並只考慮已經過去的整週。

改善和穩健性的任何建議,將不勝感激。

回答

0

當你注意你的解決方案可能會返回一個值小於週末的實際數量。

若要計算可能的額外週末,如果除以7後的總天數的剩餘天數大於0,則比較週日數(例如NSDateComponentsweekday-這是週日的7到週六的1)開始和結束日期。如果結束的週日數小於開始日期,那麼必須有一個週末之間(週六/週日的變化計算爲週末 - 如果您覺得合適的話進行調整)。

HTH

0

這裏有一個快速的實現:

NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:1209600]; 
NSDate *startDate = [NSDate date]; 
NSDateComponents *comps = [cal components:NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:startDate toDate:endDate options:kNilOptions]; 
NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; 
[fmt setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]]; 
[fmt setDateFormat:@"EEEE"]; 
NSArray *weekend = [fmt weekdaySymbols]; 
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF == 'Saturday' || SELF == 'Sunday'"]; 
NSArray *weekendArr = [weekend filteredArrayUsingPredicate:pred]; 
NSString *weekendsStr = [weekendArr componentsJoinedByString:@""]; 
NSDateComponents *compsWithDay = [[NSDateComponents alloc] init]; 
[compsWithDay setDay:1]; 
NSString *dayNameStr = [fmt stringFromDate:startDate]; 
NSUInteger count = 0; 
if ([weekendsStr rangeOfString:dayNameStr].location != NSNotFound) { 
    count++; 
} 
for (int i = 0; i < comps.day; i++) { 
    startDate = [cal dateByAddingComponents:compsWithDay toDate:startDate options:kNilOptions]; 
    dayNameStr = [fmt stringFromDate:startDate]; 
    if ([weekendsStr rangeOfString:dayNameStr].location != NSNotFound) { 
    count++; 
    } 
} 

注:

  1. 此代碼應該只與ARC啓用
  2. 也許有冗餘使用,不能看它現在。但代碼正常工作。
0

嘗試這樣,代碼的描述是否有意見: -

//Assuming this is your dates where you need to determine the weekend in between two dates 
NSString *[email protected]"10-01-2014"; 
NSString *[email protected]"19-01-2014"; 
NSDateFormatter *format=[[NSDateFormatter alloc]init]; 
[format setDateFormat:@"dd-MM-yyyy"]; 
NSDate *dt1=[format dateFromString:strDate1]; 
NSDate *dt2=[format dateFromString:strDate2]; 

//Now finding the days between two dates 
NSUInteger units = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit; 
NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
NSDateComponents *com=[cal components:units fromDate:dt1 toDate:dt2 options:0]; 
NSInteger day1=[com day]; 
int i=0; 
int j=0; 


//Now calculating how many weekends(Sat, Sun) are there in the total number of days. 
for(i=0; i<=day1; i++) 
{ 
    com=[[NSDateComponents alloc]init]; 
    [com setDay:i]; 
    NSDate *newDate = [[NSCalendar currentCalendar] 
         dateByAddingComponents:com 
         toDate:dt1 options:0]; 
    [format setDateFormat:@"EEE"]; 
    NSString *satSun=[format stringFromDate:newDate]; 
    if ([satSun isEqualToString:@"Sat"] || [satSun isEqualToString:@"Sun"]) 
    { 
     j++; 

    } 
} 
     NSLog(@"Total number of weekends found= %d",j);