2013-10-16 99 views
0

我目前有2个日期选择器,用户可以选择开始日期和结束日期。我想知道两分钟或几小时之间的差异。这些值将被存储到我假设的2个NSDate变量中?那么我怎样才能减去2个NSDates并得到一个浮点值。非常感谢你。减去2 Datepickers

我的回答

NSTimeInterval timeInseconds1 = [[startTimePicker date] timeIntervalSince1970]; 
NSTimeInterval timeInseconds2 = [[endTimePicker date] timeIntervalSince1970]; 
float difference = timeInseconds2 - timeInseconds1; 
NSLog(@"%f", difference); 

回答

1

看:

[date timeIntervalSince1970] 

将让你以秒为单位定义为双一NSTimeInterval。你可以从那里做那个数学然后使用

[NSDate dateWithTimeIntervalSince1970:time] 
+0

非常感谢你,它的工作原理!我发布了我的答案。 – 4GetFullOf