两个NSDates之间的周末数

时间:2014-01-09 23:24:06

标签: ios objective-c nsdate

假设我们使用的是格里高利日历系统,那么计算两个NSDate个对象之间发生的周末数的最佳方法是什么?

我目前的方法是计算两个NSDate之间的天数,然后除以7并floor()结果。这给了我周数,结果是周末数。因为我们floor结果如果一周尚未完成(并且该部门有剩余部分),它会忽略该结果并且仅考虑已经过去的整周。

任何有关改进和稳健性的建议都将受到赞赏。

3 个答案:

答案 0 :(得分:1)

尝试这样,代码的描述在评论中: -

//Assuming this is your dates where you need to determine the weekend in between two dates
NSString *strDate1=@"10-01-2014";
NSString *strDate2=@"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);

答案 1 :(得分:0)

如您所知,您的解决方案可能会返回比实际周末数少一个的值。

如果除以7之后的剩余总天数大于0,则考虑可能的额外周末,然后比较工作日数字(例如NSDateComponents的{​​{1}} - 周日为1开始和结束日期的7月7日)。如果结束的工作日数量少于开始工作日,那么必须有一个周末(将周六/周日的变化计算为周末 - 根据您的需要进行调整)。

HTH

答案 2 :(得分: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. 也许有冗余,现在看不到它。但代码工作正常。