Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 计算今天和未来日期之间的天数和小时数_Ios_Objective C - Fatal编程技术网

Ios 计算今天和未来日期之间的天数和小时数

Ios 计算今天和未来日期之间的天数和小时数,ios,objective-c,Ios,Objective C,**解决了。已更新代码以正确工作** 我试图计算当前日期/时间和未来日期/时间之间的日、小时、分钟和秒差 我做错了什么?首先,时区似乎工作不正常。在所有的小时、分钟和秒数中,第二个远远不够 结果: 当前日期:2013-09-1205:42:24+0000(应为太平洋标准时间晚上10:43) 未来日期:2013-09-29 23:00:00+0000(太平洋标准时间下午4点) 天数:17 小时数:596523.235278(应为~425) 会议记录:35791394(应为~25516) 秒数:21

**解决了。已更新代码以正确工作**

我试图计算当前日期/时间和未来日期/时间之间的日、小时、分钟和秒差

我做错了什么?首先,时区似乎工作不正常。在所有的小时、分钟和秒数中,第二个远远不够

结果: 当前日期:2013-09-1205:42:24+0000(应为太平洋标准时间晚上10:43) 未来日期:2013-09-29 23:00:00+0000(太平洋标准时间下午4点)

天数:17 小时数:596523.235278(应为~425) 会议记录:35791394(应为~25516) 秒数:2147483647(应为~1530960)

//检查两者之间的时间 NSDateComponents*差异=[[NSCalendar currentCalendar]组件:NSDayCalendarUnit fromDate:todaysDate toDate:newDate选项:0]; int dayDiff=[差异日]

difference = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int hourDiff = [difference hour];

difference = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int minDiff = [difference minute];

difference = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit fromDate:todaysDate toDate:newDate options:0];
int secDiff = [difference second];

self.daysLabel.text=[NSString stringWithFormat:@"%d",dayDiff];
self.hoursLabel.text=[NSString stringWithFormat:@"%d",hourDiff];
self.minutesLabel.text=[NSString stringWithFormat:@"%d",minDiff];
self.secondsLabel.text=[NSString stringWithFormat:@"%d",secDiff];

NSLog(@"\nDays: %i\nHours: %d\nMinutes: %i\nSeconds: %i",dayDiff,hourDiff,minDiff,secDiff);
试试这个

 - (NSInteger)timeDifference:(NSDate *)fromDate ToDate:(NSDate *)toDate{

    HMLogger_Info(@"<INPUT PARAMETERS> FromDate: %@, ToDate: %@",fromDate,toDate);

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *differenceValue = [calendar components:NSHourCalendarUnit
                                                    fromDate:fromDate toDate:toDate options:0];
    HMLogger_Info(@"Calculated hour difference : %d",[differenceValue hour]);
    NSInteger hourDifference = [differenceValue hour];

    HMLogger_Info(@"<RETURN VALUE>: HourDifference: %d",hourDifference);
    return hourDifference;
}
-(NSInteger)时差:(NSDate*)从日期到日期:(NSDate*)到日期{
HMU信息(@“FromDate:%@,ToDate:%@”,FromDate,ToDate);
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*differenceValue=[日历组件:NSHourCalendarUnit
fromDate:fromDate toDate:toDate选项:0];
HMLogger_信息(@“计算小时差:%d”,[differenceValue hour]);
NSInteger hourDifference=[差异值小时];
HMU信息(@):小时差异:%d”,小时差异);
返回时差;
}
试试这个

 - (NSInteger)timeDifference:(NSDate *)fromDate ToDate:(NSDate *)toDate{

    HMLogger_Info(@"<INPUT PARAMETERS> FromDate: %@, ToDate: %@",fromDate,toDate);

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *differenceValue = [calendar components:NSHourCalendarUnit
                                                    fromDate:fromDate toDate:toDate options:0];
    HMLogger_Info(@"Calculated hour difference : %d",[differenceValue hour]);
    NSInteger hourDifference = [differenceValue hour];

    HMLogger_Info(@"<RETURN VALUE>: HourDifference: %d",hourDifference);
    return hourDifference;
}
-(NSInteger)时差:(NSDate*)从日期到日期:(NSDate*)到日期{
HMU信息(@“FromDate:%@,ToDate:%@”,FromDate,ToDate);
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*differenceValue=[日历组件:NSHourCalendarUnit
fromDate:fromDate toDate:toDate选项:0];
HMLogger_信息(@“计算小时差:%d”,[differenceValue hour]);
NSInteger hourDifference=[差异值小时];
HMU信息(@):小时差异:%d”,小时差异);
返回时差;
}
我认为它有助于您使用calendar*calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]
NSDateComponents*components=[日历组件:NSDayCalendarUnit | NSHourCalendarUnit fromDate:fromDate toDate:toDate选项:0]
NSLog(@“开始日期和结束日期之间的差异为%d天和%d小时”,组件.day,组件.hour)

我认为它有助于您使用calendar*calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]
NSDateComponents*components=[日历组件:NSDayCalendarUnit | NSHourCalendarUnit fromDate:fromDate toDate:toDate选项:0]
NSLog(@“开始日期和结束日期之间的差异为%d天和%d小时”,组件.day,组件.hour)

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit|NSHourCalendarUnit fromDate:fromDate toDate:toDate options:0];
NSLog(@"The difference between from date and to date is %d days and %d hours",components.day,components.hour); 输出: DateBookApp[1179:70b]从日期到日期的差异是0天22小时57分12秒

输出:
DateBookApp[1179:70b]从日期到日期的差异是0天22小时57分12秒

搞定了!谢谢我的问题是单位的问题。我在三次计算中都使用了单位。用下面的代码更新了原始代码。搞定了!谢谢我的问题是单位的问题。我在三次计算中都使用了单位。用以下代码更新了原始代码。 NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit|NSHourCalendarUnit fromDate:fromDate toDate:toDate options:0];
NSLog(@"The difference between from date and to date is %d days and %d hours",components.day,components.hour);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *firstDate = @"2014-06-05 12:55:00";

NSDate *date=[dateFormatter dateFromString:firstDate];

NSDate *currentDate = [NSDate date];

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:date toDate:currentDate options:0];

NSLog(@"The difference between from date and to date is %d days and %d hours and %d minute and %d second",components.day,components.hour,components.minute,components.second);