【问题标题】:How to calculate count of particular day between two dates?如何计算两个日期之间特定日期的计数?
【发布时间】:2014-10-04 09:16:49
【问题描述】:

我想要两个日期之间特定日期的总计数。我想做一个返回特定日期总计数的函数。 例如:如果我通过了星期一、开始日期和结束日期,那么它将返回这些日期之间的星期一总数。

如果我通过星期一作为开始日期并且开始日期是 2014-10-04 并且结束日期是 2014-10-18 那么函数应该是返回星期一的计数是 2

【问题讨论】:

    标签: ios iphone date calendar


    【解决方案1】:

    我可以告诉你一个简单的计算逻辑。如果是星期一(开始日期 A),下一个星期一将在 7 天后......所以在开始日期上添加 7 天,并检查下一个日期(开始日期 B)是否小于结束日期,如果不是,再次添加 7到(开始日期 B)的天数再次检查这是否小于结束日期。相应地计算它。做这样的事情

        NSDateComponents *dateComponents = [NSDateComponents new];
            dateComponents.day = 7;
            NSDate *DateWithAdditon = [[NSCalendar currentCalendar]dateByAddingComponents:dateComponents   toDate: StartDate
                                                                          options:0];
    
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
            [dateFormatter setDateFormat:@"dd/MM/yyyy"];
            [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
            NSString *DateWithAdditonString=  [ dateFormatter stringFromDate:DateWithAdditon];
    
            NSLog(@"your next monday will be here %@ ",DateWithAdditonString );
    
    
    //Write code to check wheather the DateWith addition is lesser than the End date if not add another 7 days and increase the count accordingly
    

    【讨论】:

      【解决方案2】:

      我自己解决了

      -(int)countDays:(int)dayCode startDate:(NSDate *)stDate endDate:(NSDate *)endDate 
      {
      
          // day code is Sunday = 1 ,Monday = 2,Tuesday = 3,Wednesday = 4,Thursday = 5,Friday = 6,Saturday = 7
      
          NSInteger count = 0;
      
          // 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 = stDate;
      
          // Iterate from fromDate until toDate
          while ([currentDate compare:endDate] == NSOrderedAscending) {
      
              NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];
      
              if (dateComponents.weekday == dayCode) {
                  count++;
              }
      
              // "Increment" currentDate by one day.
              currentDate = [calendar dateByAddingComponents:oneDay
                                                  toDate:currentDate
                                                 options:0];
          }
          NSDateComponents* component = [calendar components:NSWeekdayCalendarUnit fromDate:endDate];
          int weekDay = [component weekday];
          if (weekDay == dayCode) { // Condition if end date contain your day then count should be increase
              count ++ ;
          }
          return count; // Return your day count
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多