相关疑难解决方法(0)

如何比较两个日期,返回几天

我怎样才能比较两个日期的返回天数.例如:缺少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)  
Run Code Online (Sandbox Code Playgroud)

compare objective-c days

22
推荐指数
1
解决办法
6733
查看次数

iPhone - 获取两个日期之间的天数

我正在为iPhone编写一个GTD应用程序.对于应有的任务,我希望显示诸如"明天到期"或"到期日"或"到期7月18日"之类的内容.显然,即使任务距离不到24小时,我也需要显示"明天"(例如,用户在星期六晚上11点检查并看到周日早上8点有任务).所以,我写了一个方法来获取两个日期之间的天数.这是代码......

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm"];

NSDate *nowDate = [dateFormatter dateFromString:@"2010-01-01-15-00"];
NSDate *dueDate = [dateFormatter dateFromString:@"2010-01-02-14-00"];

NSLog(@"NSDate *nowDate = %@", nowDate);
NSLog(@"NSDate *dueDate = %@", dueDate);

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *differenceComponents = [calendar components:(NSDayCalendarUnit)
                                                     fromDate:nowDate
                                                       toDate:dueDate
                                                      options:0];

NSLog(@"Days between dates: %d", [differenceComponents day]);
Run Code Online (Sandbox Code Playgroud)

......这是输出:

NSDate *nowDate = 2010-01-01 15:00:00 -0700
NSDate *dueDate = 2010-01-02 14:00:00 -0700
Days between dates: 0
Run Code Online (Sandbox Code Playgroud)

如您所见,该方法返回不正确的结果.它应该返回1作为两天之间的天数.我在这做错了什么?

编辑:我写了另一种方法.我没有进行过广泛的单元测试,但到目前为止似乎有效:

+ (NSInteger)daysFromDate:(NSDate *)fromDate inTimeZone:(NSTimeZone *)fromTimeZone untilDate:(NSDate *)toDate inTimeZone:(NSTimeZone *)toTimeZone {

    NSCalendar *calendar = …
Run Code Online (Sandbox Code Playgroud)

iphone cocoa cocoa-touch nsdate nsdatecomponents

5
推荐指数
1
解决办法
6538
查看次数

如何获得两个日期之间的天数objective-c

我正在尝试制作一个标签,说明事件发生了多少天.我想计算今天的日期和事件日期之间的差异.我正在使用此代码,它给我-4600.它工作正常,直到我使用今天的日期.

NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [NSDate date];
    NSDate *endDate = [f dateFromString:end];
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                        fromDate:startDate
                                                          toDate:endDate
                                                         options:0];
    return components.day;
return components.day;
Run Code Online (Sandbox Code Playgroud)

objective-c nsdate nsstring ios

3
推荐指数
1
解决办法
1万
查看次数