显示null的日期比较

时间:2016-10-19 10:46:21

标签: objective-c nsdate nsdateformatter

我有两个这样的日期,

NSString *givendate = @"2016-10-07T13:01:20";
NSDate *currentDateTime = [NSDate date];

我想通过将当前日期与给定日期进行比较来计算5天或6天的天数。

我尝试了以下代码。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFromString = [dateFormatter dateFromString:givendate];
NSTimeInterval diff = [currentDateTime timeIntervalSinceDate:dateFromString];
NSString *intervalString = [NSString stringWithFormat:@"%f", diff];
cell.days.text=intervalString;

但它打印“nan”。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

您必须根据下面的日期字符串为dateFormat指定NSDateFormatter,否则它将返回nilnan

    NSDate *dateFromString;

    NSString *givendate = @"2016-10-07T13:01:20";
    NSDate *currentDateTime = [NSDate date];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];//Set format here accordingly
    dateFromString = [dateFormatter dateFromString:givendate];
    NSTimeInterval diff = [currentDateTime timeIntervalSinceDate:dateFromString];
    NSString *intervalString = [NSString stringWithFormat:@"%f", diff];

上面的代码对我有用。

<强> 编辑:

int num_days = diff / (60 * 60 * 24);
int num_seconds -= num_days * (60 * 60 * 24);
int num_hours = num_seconds / (60 * 60);
int num_seconds -= num_hours * (60 * 60);
int num_minutes = num_seconds / 60;

OR

使用NSGregorianCalendar,如下所示:

    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];

    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags
                                            fromDate:dateFromString
                                              toDate:currentDateTime options:0];
    NSInteger years = [components year];
    NSInteger months = [components month];
    NSInteger days = [components day]; 
    NSLog(@"years = %ld months = %ld days = %ld",years,months,days);

答案 1 :(得分:0)

我不确定,但也许是因为你没有设置日期格式:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; //According to date and time format;
dateFromString = [dateFormatter dateFromString:givendate];
NSTimeInterval diff = [currentDateTime timeIntervalSinceDate:dateFromString];
NSString *intervalString = [NSString stringWithFormat:@"%f", diff];
cell.days.text=intervalString;

答案 2 :(得分:0)

For&#34;它打印1050035.00这样。但我需要像这样的5天或6天。有什么帮助吗?&#34;

因为它以毫秒为单位返回,所以您需要将其转换为几天。

NSCalendar *_calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSCalendarUnit _units = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *_components = [_calendar components:_units fromDate:[NSDate date] toDate:[NSDate dateWithTimeIntervalSinceNow:_timeInSeconds] options:kNilOptions];

NSLog(@"%ld Days, %ld Hours, %ld Minutes, %ld Seconds", _components.day, _components.hour, _components.minute, _components.second);

在您的情况下,输出:12天,3小时,40分钟,35秒

相关问题