2013-10-16 100 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