【问题标题】:Find exact difference between two dates找出两个日期之间的确切差异
【发布时间】:2011-05-31 11:21:18
【问题描述】:

我想要对日期比较进行一些更改。

在我的应用程序中,我比较了两个日期并将差异作为天数,但如果只有一天差异,系统会将 0 显示为天数差异。

NSDateFormatter *date_formater=[[NSDateFormatter alloc]init];
[date_formater setDateFormat:@"MMM dd,YYYY"];

NSString *now=[NSString stringWithFormat:@"%@",[date_formater stringFromDate:[NSDate date]]];
LblTodayDate.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",now]];

NSDate *dateofevent = [[NSUserDefaults standardUserDefaults] valueForKey:@"CeremonyDate_"];
NSDate *endDate =dateofevent;

NSDate *startDate = [NSDate date];

gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];

int days = [components day];

我找到了一些解决方案,如果我们将时间设为 00:00:00 进行比较,那么它会告诉我正确的答案,我不知道是对还是错。

【问题讨论】:

    标签: iphone nsstring nsdate


    【解决方案1】:

    我通常以秒为单位找出差异并计算ceil(diffInSeconds / 86400)。

    【讨论】:

      【解决方案2】:

      我认为这是不正确的,将时间设为 00:00:00。

      可能你得到的差异小于 24 小时,这就是它四舍五入而你 0 天的原因。

      Alexander 解决方案是正确的,因此请使用该解决方案 -

      这对我也很好。

      NSDate *endDate=[dateFormat dateFromString:now];
              NSTimeInterval interval = [CeremonyDate timeIntervalSinceDate:endDate];
              int diff=interval/86400;//for converting seconds into days.
      

      同样的问题是对你得到的数字进行四舍五入,但你可以用一种不稳定的方式来解决这个问题。

      【讨论】:

        【解决方案3】:

        试试这个代码以获得两个不同的日期和时间。

          NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
                [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
                [dateFormatter setDateFormat:@"mm:ss"];
        
                NSDate* firstDate = [dateFormatter dateFromString:@"04:45"];
                NSDate* secondDate = [dateFormatter dateFromString:@"05:00"];
        
                NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
                NSLog(@"%f",timeDifference);
        

        我希望这段代码对你有用。

        【讨论】:

          【解决方案4】:

          这里是查找两个日期之间差异的完美解决方案

          - (NSString *)calculateDuration:(NSDate *)oldTime secondDate:(NSDate *)currentTime
          {
              NSString *timeSincePost;
           NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
              NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
              NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:oldTime toDate:currentTime options:0];
          
              NSInteger year = [dateComponents year];
              NSInteger month = [dateComponents month];
              NSInteger day = [dateComponents day];
              NSInteger hour = [dateComponents hour];
              NSInteger minute = [dateComponents minute];
              NSInteger second = [dateComponents second];
          
              if (year) {
                  timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)year,[[kAppDelegate languageBundle] localizedStringForKey:@"y" value:@"" table:nil]];
              }
              else if (month) {
                   timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)month,[[kAppDelegate languageBundle] localizedStringForKey:@"M" value:@"" table:nil]];
              }
              if(day) {
                  timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)day,[[kAppDelegate languageBundle] localizedStringForKey:@"d" value:@"" table:nil]];
              }
              else if(hour) {
                  timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)hour,[[kAppDelegate languageBundle] localizedStringForKey:@"H" value:@"" table:nil]];
              }
              else if(minute) {
                  timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)minute,[[kAppDelegate languageBundle] localizedStringForKey:@"m" value:@"" table:nil]];
              }
              else if(second)
                  timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)second,[[kAppDelegate languageBundle] localizedStringForKey:@"s" value:@"" table:nil]];
          
              return timeSincePost;
          }
          

          并使用两个参数作为 NSDate 调用上述函数

          NSString *duration = [self calculateDuration:postDate secondDate:[NSDate date]];
          lblPostTime.text = duration;
          

          注意:: postDate 是 FirstDate & second date 是当前日期..

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-08-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-09
            • 2011-10-29
            • 1970-01-01
            相关资源
            最近更新 更多