timeIntervalSinceDate 未返回正确答案 objective c
timeIntervalSinceDate not returning correct answer objective c
我正在尝试让 timeIntervalSinceDate
以秒为单位发送日志,记录自上一个日期以来已经过去了多少时间,但它不起作用。我每次测试它时都会将它设置为 1 分钟,但我没有得到对应的日志,就像最新的日志是这样的:2015-09-22 16:30:04.469 Wakey Wakey[49785:7077539] Seconds nan 2015-09-22 16:30:04.470
。这是代码:
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date ];
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd:MM:YYYY:ss:mm:hh"];
NSDate *dateTimeSeconds = [[NSDate alloc] init];
dateTimeSeconds = [dateFormatter1 dateFromString:dateTimeString];
NSTimeInterval secs = [currentTime timeIntervalSinceDate:dateTimeSeconds];
NSLog(@"Seconds %g", secs);
我应该怎么做才能解决它?
试试这个
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
为什么要使用 NSDateFormatter。尝试
NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:dateTimePicker.date];
NSLog(@"seconds %.f", seconds);
在 NSLog 上也使用 %.f 而不是 %g
请尝试使用以下日期格式
kTimeStampFormat @"dd-MM-yyyy'T'HH:mm:ss.SSS'Z'"
为保持一致性使用 UTC 格式
我正在尝试让 timeIntervalSinceDate
以秒为单位发送日志,记录自上一个日期以来已经过去了多少时间,但它不起作用。我每次测试它时都会将它设置为 1 分钟,但我没有得到对应的日志,就像最新的日志是这样的:2015-09-22 16:30:04.469 Wakey Wakey[49785:7077539] Seconds nan 2015-09-22 16:30:04.470
。这是代码:
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date ];
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd:MM:YYYY:ss:mm:hh"];
NSDate *dateTimeSeconds = [[NSDate alloc] init];
dateTimeSeconds = [dateFormatter1 dateFromString:dateTimeString];
NSTimeInterval secs = [currentTime timeIntervalSinceDate:dateTimeSeconds];
NSLog(@"Seconds %g", secs);
我应该怎么做才能解决它?
试试这个
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
为什么要使用 NSDateFormatter。尝试
NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:dateTimePicker.date];
NSLog(@"seconds %.f", seconds);
在 NSLog 上也使用 %.f 而不是 %g
请尝试使用以下日期格式
kTimeStampFormat @"dd-MM-yyyy'T'HH:mm:ss.SSS'Z'"
为保持一致性使用 UTC 格式