【问题标题】:How can I compare two dates, return a number of days如何比较两个日期,返回天数
【发布时间】:2011-02-02 15:07:29
【问题描述】:

如何比较两个日期返回天数。 例如:缺席 X 天的杯赛。 看看我的代码。

  NSDateFormatter *df = [[NSDateFormatter alloc]init];  
  [df setDateFormat:@"d MMMM,yyyy"];  
  NSDate *date1 = [df dateFromString:@"11-05-2010"];  
  NSDate *date2 = [df dateFromString:@"11-06-2010"];  
  NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];  
  //int days = (int)interval / 30;  
  //int months = (interval - (months/30)) / 30;  
  NSString *timeDiff = [NSString stringWithFormat:@"%dMissing%d days of the Cup",date1,date2, fabs(interval)];  

  label.text = timeDiff; // output (Missing X days of the Cup)  

【问题讨论】:

标签: objective-c compare days


【解决方案1】:

试试这个类别:

@interface NSDate (DateUtils)

-(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate; 

@end

@implementation NSDate (DateUtils)

-(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate
{

    NSCalendar *calendar = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *components = [calendar components:NSCalendarUnitDay
                                                fromDate:self
                                                  toDate:aDate options:kNilOptions];

    return [components day];

}

@end

您可以通过添加导入来使用此类别:

#import "NSDate+DateUtils.h"

并从您的代码中调用它:

NSInteger days = [myDate numberOfDaysUntilDay:someOtherDate];

【讨论】:

    【解决方案2】:

    需要调用此方法并设置 2 个我们想要以不同方式计算的日期。

    -(void) calculateSleepHours:(NSDate *)sleepDate :(NSDate *)wakeStr 
    {
    
       if (sleepDate !=nil && wakeStr!=nil) {`enter code here`
    
        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
        [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ssZ"];
        NSDate *date1 = [ApplicationManager getInstance].sleepTime;;
    
        NSDate *date2 = [ApplicationManager getInstance].wakeupTime;
    
        NSCalendar *gregorian = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    
        NSDateComponents *components = [gregorian components:unitFlags
                                                    fromDate:date1
                                                      toDate:date2 options:0];
    
        NSInteger months = [components month];
        NSInteger days = [components day];
        NSInteger hours = [components hour];
        NSInteger minute=[components minute];
        NSInteger second=[components second];
        DLog(@"Month %ld day %ld hour is %ld min %ld sec %ld  ",(long)months,(long)days,(long)hours,(long)minute,(long)second);
    
        sleepHours.text=[NSString stringWithFormat:@"Hour %ld Min %ld Sec %ld",(long)hours,(long)minute,(long)second];
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用下一个类别:

      @interface NSDate (Additions)
      
      -(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate; 
      -(NSInteger)numberOfHoursUntilDay:(NSDate *)aDate;
      
      @end
      
      @implementation NSDate (Additions)
      const NSInteger secondPerMunite = 60;       
      const NSInteger munitePerHour = 60;
      const NSInteger hourPerDay = 24;
      
      -(NSInteger)numberOfDaysUntilDay:(NSDate *)aDate 
      {
          NSInteger selfTimeInterval = [aDate timeIntervalSinceDate:self];   
          return abs(selfTimeInterval / (secondPerMunite * munitePerHour * hourPerDay));    
      }
      
      -(NSInteger)numberOfHoursUntilDay:(NSDate *)aDate 
      {
          NSInteger selfTimeInterval = [aDate timeIntervalSinceDate:self];
          return abs(selfTimeInterval / (secondPerMunite * munitePerHour));
      }
      @end
      

      【讨论】:

      • 这是一个非常错误的方法。请改用 NSCalendar 方法。
      【解决方案4】:

      来自Apple's example,基本上使用NSCalendar:

      NSDate * date1 = <however you initialize this>;
      NSDate * date2 = <...>;
      
      NSCalendar *gregorian = [[NSCalendar alloc]
                       initWithCalendarIdentifier:NSGregorianCalendar];
      
      NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
      
      NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:date1
                                                toDate:date2 options:0];
      
      NSInteger months = [components month];
      NSInteger days = [components day];
      

      【讨论】:

      • 我第一次错过了。这可能就是他没有得到他想要的东西的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多