两个日期之间的时间间隔返回nil

曼尼什·沙玛(Maneesh Sharma)

我正在计算两个NSDate之间的时差,它始终返回0.0000。我正在使用相同的逻辑来计算时间戳,并且格式正确。.当我尝试分别记录日期时,它会正确打印,当我打印差异,打印0.0000 ...这是计算日期的代码..

-(NSDate *) toLocalTime
{
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: [NSDate date]];
return [NSDate dateWithTimeInterval: seconds sinceDate: [NSDate date]];
}

这是我发现不同之处的代码。

NSLog(@"the time when the location was updated is %@",timeWhenTheLocationWasUpdated);
NSLog(@"the time when the server was contacted last time was %@",timeWhenLastLocationPosted);

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss ZZZ"];
NSDate *timeofUpdatingLocation = [[NSDate alloc] init];
NSDate *timeofPostingToTheServer = [[NSDate alloc]init];
timeofUpdatingLocation = [dateFormatter dateFromString:timeWhenTheLocationWasUpdated];
timeofPostingToTheServer = [dateFormatter dateFromString:timeWhenLastLocationPosted];
NSTimeInterval difference = [timeofPostingToTheServer timeIntervalSinceDate:timeofUpdatingLocation];
NSLog(@"the difference between posting the location to the server and updating the location is %f",difference);

代码开头的两个NSlog均以日期格式化程序中提到的格式正确打印日期。

邓肯

在Objective-C中,将消息发送到零个对象是完全合法的。您返回0 / nil / FALSE。

NSdateFormatterdateFromString如果字符串与格式不匹配对象从调用返回nil

我的猜测是,您对的调用dateFromString失败并且timeofPostingToTheServer为零,因此您将返回0。

(并且作为脚注,正如rmaddy在他的评论中所建议的那样,toLocalTime对于您要解决的问题似乎没有意义)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

返回两个日期时间表达式之间的差

两个表之间的交集间隔日期

如何获取两个时间间隔之间的星期五日期

给定两个间隔的排序列表,返回两个列表之间的重叠间隔

在VBA中创建两个日期之间的间隔列表

如何基于两个时间间隔之间的日期合并数据帧?

如何获取两个小时间隔的日期时间?

查找两个间隔之间的间隔

计算两个时间戳记之间的时间间隔(分钟)

两个日期时间之间的差

如何生成两个日期之间的日期时间间隔列表?

检查两个日期之间的间隔天数(失败)

如何计算两个“ keyup”事件之间的时间间隔

在两个日期之间的MySQL或PHP中生成50个随机持续时间间隔

不同单元格的两个日期之间的间隔

查找间隔为n的两个日期之间的行

给出两个连续的Javafx动画之间的时间间隔

检查时间是否在两个间隔之间

返回两个日期之间的数据

从两个表中查找日期时间之间的时间间隔

从两个日期时间之间的时差返回另一个时间

PHP的两个日期之间的时间是

两个日期之间的快速分钟间隔

获取两个日期之间的时间

在两个日期对象之间以 30 分钟间隔获取时间的 Javascript 编程方式

两个日期时间之间的 15 分钟间隔数

基于 SQL 中两个给定日期之间的给定间隔返回日期范围

两个时间戳之间的 30 分钟间隔数

计算 DataFrame 中两个给定日期值之间的特定时间间隔