我正在尝试查找 2 个日期之间的时差。但我得到的值为-0.0000
,nan
或-357683564
. 下面是我查找时差的代码。我在这个计算中做错了吗?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeStyle = NSDateFormatterNoStyle;
formatter.dateFormat = @"YYYY-MM-dd HH:MM:SS ±HHMM";//MM/dd/yyyy
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale:usLocale];
NSDate *intialTime= [formatter dateFromString:[NSString stringWithFormat:@"%@",[dateArray objectAtIndex:i]]];//[formatter dateFromString:@"12/11/2005"];
NSDate *presentDate = [NSDate date];
NSTimeInterval timeInterval = [presentDate timeIntervalSinceDate:intialTime];
int seconds = (int)ceil(timeInterval);
int mins = seconds / 60;
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = timeInterval / secondsInAnHour;
NSLog(@"Time interval in Mins:%d -- %.4f -- %d",mins,timeInterval,hoursBetweenDates);
在日志中:
Time interval in Mins:-35791394 -- nan -- -2147483648
intialTime - (null) and presentDate - 2014-05-09 10:30:15 +0000