【问题标题】:How to get the difference between two dates?如何获得两个日期之间的差异?
【发布时间】:2010-03-17 12:42:56
【问题描述】:

我有两个日期格式(MM/dd/yyyy hh:mm:ss:SS)。对于这两个日期,我使用 (stringFromDate) 方法将两个日期转换为字符串。但我无法区分它们并在我的控制台中显示它们。请给我一个想法,我应该如何得到它? 谢谢。

【问题讨论】:

    标签: cocoa-touch iphone-sdk-3.0 cocos2d-iphone


    【解决方案1】:

    例子

        NSDate *today = [NSDate date];
    
    NSTimeInterval dateTime;
    
    
    if ([visitDate isEqualToDate:today])   //visitDate is a NSDate
    
    {
    
    NSLog (@"Dates are equal");
    
    }
    
    dateTime = ([visitDate timeIntervalSinceDate:today] / 86400);  
    
    if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val
    
    {
    
    NSLog (@"Past Date");
    
    }
    
    else 
    
    {   
    NSLog (@"Future Date");
    
    }
    

    【讨论】:

    • 我建议将这个幻数 (86400) 放在一个常量中,如下所示:const CGFloat kSecondsPerDay = 60 * 60 * 24;..
    【解决方案2】:

    将日期保留为日期,获取它们之间的差异,然后打印差异。

    来自docs on NSCalendar 并假设 gregorian 是 NSCalendar:

    NSDate *startDate = ...;
    
    NSDate *endDate = ...;
    
    unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
    
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];
    
    int months = [comps month];
    
    int days = [comps day];
    

    【讨论】:

      【解决方案3】:

      通常我看到通过将日/年值转换为平日(通常是自一些开始 epoch 以来的天数,如 1970 年 1 月 1 日)来处理日增量计算。

      为了帮助解决这个问题,我发现创建一个包含每个月开始的一年中的天数的表格很有帮助。这是我最近使用的一个类。

      namespace {
          // Helper class for figuring out things like day of year
          class month_database {
          public:
              month_database () {
      
                  days_into_year[0] = 0;
                  for (int i=0; i<11; i++) {
                      days_into_year[i+1] = days_into_year[i] + days_in_month[i];
                  }
              };
      
              // Return the start day of the year for the given month (January = month 1).
              int start_day (int month, int year) const {
      
                  // Account for leap years. Actually, this doesn't get the year 1900 or 2100 right,
                  // but should be good enough for a while.
                  if ( (year % 4) == 0 && month > 2) {
                      return days_into_year[month-1] + 1;
                  } else {
                      return days_into_year[month-1];
                  }
              }
          private:
              static int const days_in_month[12];
      
              // # of days into the year the previous month ends
              int days_into_year[12];
          };
          // 30 days has September, April, June, and November...
          int const month_database::days_in_month[12] = {31, 28, 31, 30,   31, 30, 31, 31,   30, 31, 30, 31};
      
          month_database month;
      }
      

      start_day 方法中可以看出,您要解决的主要问题是您的范围内包含多少个闰日。在我们的时代,我使用的计算已经足够好了。年份包含闰日的实际规则是discussed here

      公历 2 月 29 日, 今天使用最广泛的,是日期 每四次才发生一次 年,以能被 4 整除的年数, 如 1976、1996、2000、2004、2008、 2012 年或 2016 年(除了 世纪年不能被400整除, 例如 1900)。

      【讨论】:

        【解决方案4】:

        如果您只想要天数的差异,您可以这样做。 (基于 mihir mehta 的回答。)

        const NSTimeInterval kSecondsPerDay = 60 * 60 * 24;
        - (NSInteger)daysUntilDate:(NSDate *)anotherDate {
            NSTimeInterval secondsUntilExpired = [self timeIntervalSinceDate:anotherDate];
            NSTimeInterval days = secondsUntilExpired / kSecondsPerDay;
            return (NSInteger)days;
        }
        

        【讨论】:

        • (你应该把它作为一个类别方法添加到 NSDate。)
        猜你喜欢
        • 1970-01-01
        • 2018-02-06
        • 2014-03-15
        • 1970-01-01
        • 1970-01-01
        • 2022-12-01
        • 2021-08-27
        相关资源
        最近更新 更多