我有一個日期字符串,我試圖轉換成NSDate比較它與當前dateTime。我試圖實現這一點,但創建一個空值爲IM即將嘗試轉換的日期。從字符串獲取NSDate
這是我試過的。
-(BOOL)checkTime{
//create an instance of the database oject
DataBase * dataBase = [[DataBase alloc] init];
//get the times
NSMutableArray *times = [dataBase allDataFrom:@"timings"];
//set date time to string and refresh time to float.
NSString *dateStr = times[1];
float refreshTime = [times[2] floatValue];
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
//get the time now
NSDate* timeNow = [NSDate date];
NSLog(@"dateStr: %@",dateStr);
NSLog(@"DataBase Date: %@",date);
NSLog(@"Current Date: %@",timeNow);
NSLog(@"Time until refresh. (seconds): %f", refreshTime);
// If date is greater than refresh then update the data
if ([timeNow timeIntervalSinceDate:date] > refreshTime)
{
NSLog(@"Data Needs to be checked.");
return YES;
}else{
NSLog(@"Data is fresh.");
return NO;
}
}
這是林從記錄我的所有值
dateStr: 2013/02/26 12:39:28
DataBase Date: (null)
Current Date: 2013-02-26 12:57:49 +0000
Time until refresh. (seconds): 30.000000
Data is fresh.
讓我有語法不正確或莫不是別的東西,我錯過了什麼?
顯示的'times'數組中的值? – graver 2013-02-26 14:08:54
你見過這個有用的鏈接http://www.ios-developer.net/iphone-ipad-programmer/development/date-and-time/date-and-time-examples – topcat3 2013-02-26 14:09:58