2013-12-09 44 views
0

我想將字符串轉換爲日期時間對象,然後減去2個日期之間的差異。不過,我不斷收到一個無效的指針警告。日期格式爲20131209 02:34。我的代碼如下將字符串轉換爲日期並找出時差

NSDateFormatter *df =[[NSDateFormatter alloc]init]; 
[df setDateFormat:@"YYYYMMDD HH:MM"]; 
NSDate *currTime = [df dateFromString:self.currentTime]; 
NSDate *predicTime =[df dateFromString:self.predictedTime]; 
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime]; 
+0

YYdd HH:MM是這個有效的日期格式? –

+1

'[df setDateFormat:@「yyyyMMdd hh:mm」];' – Maulik

+0

'self.predictedTime'和'self.currentTime'指向'NSString'實例的有效指針嗎? – FluffulousChimp

回答

1

您的日期格式是不正確的[df setDateFormat:@"YYYYMMDD HH:MM"];我假設你的字符串是有效的!

嘗試:

NSDateFormatter *df =[[NSDateFormatter alloc]init]; 
[df setDateFormat:@"yyyyMMdd HH:mm"]; // 24hr format, for 12hr format use 'hh' 
NSDate *currTime = [df dateFromString:@"20131209 02:34"]; 
NSDate *predicTime =[df dateFromString:@"20131209 04:34"]; 
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime]; 
+0

我已經試過這個解決方案,我仍然得到相同的錯誤 –

+0

更可能是24小時的「HH」。 –

+0

@TroyBarrios:然後顯示你的錯誤!什麼錯誤? – Maulik

0

嘗試這個代碼

NSDateFormatter *df =[[NSDateFormatter alloc]init]; 
[df setDateFormat:@"yyyyMMdd hh:mm"]; 
NSDate *currTime = [df dateFromString:self.currentTime]; 
NSDate *predicTime =[df dateFromString:self.predictedTime]; 
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];