【发布时间】:2013-06-21 10:24:52
【问题描述】:
我正在开发测验应用程序。在我的应用程序中,我想了解测验开始时间和结束时间之间的差异。如何获取开始时间和结束时间之间的差异。
提前致谢。
【问题讨论】:
-
Google 可以很快找到您的答案!你浪费时间在这里发布问题。
标签: iphone ios objective-c datetime
我正在开发测验应用程序。在我的应用程序中,我想了解测验开始时间和结束时间之间的差异。如何获取开始时间和结束时间之间的差异。
提前致谢。
【问题讨论】:
标签: iphone ios objective-c datetime
您可以为此目的使用 NSDate - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate。
【讨论】:
使用这个
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yy"];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yy"];
NSString *dateString11 = [dateFormat stringFromDate:today];
NSDate *today1 = [dateFormatter1 dateFromString:dateString11];
NSDate *ExpDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"ExpDate"];
NSTimeInterval interval = [ExpDate timeIntervalSinceDate:today1];
int diff=interval/86400;
我希望这段代码对你有用。
还有另一个代码给你。
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"];
NSDate *ServerDate = [dateFormatter1 dateFromString:@"04/26/2013"];
[dateFormatter1 release];
NSTimeInterval interval = [date timeIntervalSinceDate:ServerDate];
int diff=interval/86400;
NSLog(@"%d",diff);
【讨论】:
86400 错了。
NSDate 类有一个方法 timeIntervalSinceDate 可以解决问题。
NSTimeInterval interval = [firstDate timeIntervalSinceDate:secondDate];
【讨论】: