Iphone 倒计时计时器实施的两个日期之间的差异
两个日期之间的差异怎么可能是错误的(2013-04-04T19:14:58+02:00-2013-04-03T18:14:58+02:00) 代码的结果是 01天06小时46分02秒 结果是完全错误的正确答案应该是 01天01小时00分00秒 你能帮我找出问题的根源吗 守则:Iphone 倒计时计时器实施的两个日期之间的差异,iphone,ios,objective-c-2.0,Iphone,Ios,Objective C 2.0,两个日期之间的差异怎么可能是错误的(2013-04-04T19:14:58+02:00-2013-04-03T18:14:58+02:00) 代码的结果是 01天06小时46分02秒 结果是完全错误的正确答案应该是 01天01小时00分00秒 你能帮我找出问题的根源吗 守则: -(void)startTimer { timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(upd
-(void)startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}
-(void) updateCountdown
{
NSDate *expirydate = [MSharedFunctions SqldateStringTodate:@"2013-04-04T19:14:58+02:00"];
NSDate *now = [MSharedFunctions SqldateStringTodate:@"2013-04-03T18:14:58+02:00"];
if ([expirydate isEarlierThanDate:[MSharedFunctions SqldateStringTodate:[MSharedFunctions SqldateStringTodateNow]]]) {
//Fire end counter
//[[NSNotificationCenter defaultCenter] postNotificationName:@"TIMER" object:nil];
}
//NSDate *dateFromString = [[NSDate alloc] init];
NSDate *dateFromString =expirydate;
//NSLog(@"now %@: endtime :%@ ",[MSharedFunctions SqldateStringTodateNow],@"2013-04-03T18:14:58+02:00");
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsHours = [calendar components:NSHourCalendarUnit fromDate:now];
NSDateComponents *componentMint = [calendar components:NSMinuteCalendarUnit fromDate:now];
NSDateComponents *componentSec = [calendar components:NSSecondCalendarUnit fromDate:now];
NSDateComponents *componentsDaysDiff = [calendar components:NSDayCalendarUnit
fromDate:now
toDate:dateFromString
options:0];
NSLog(@"%@",[NSString stringWithFormat:@"%02d",componentsDaysDiff.day]);
NSLog(@"%@",[NSString stringWithFormat:@"%02d",(24-componentsHours.hour)]);
NSLog(@"%@",[NSString stringWithFormat:@"%02d",(60-componentMint.minute)]);
NSLog(@"%@",[NSString stringWithFormat:@"%02d",(60-componentSec.second)]);
}
SqldateStringTodate函数:
+(NSDate*)SqldateStringTodate:(NSString*) stringdate {
stringdate = [stringdate stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange([stringdate length] - 5,5)];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
NSLocale* formatterLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] ;
[dateFormatter setLocale:formatterLocale];
if (stringdate.length==24) {
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
}
else {
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
}
//DebugLog(@"New Date %@",stringdate);
NSDate *getDate = [dateFormatter dateFromString:stringdate];
return getDate;
}
我无法理解您的逻辑,但如果您希望以您想要的方式获得差异,如
NSDateComponents
,您可以使用:
NSUInteger unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *componentsDaysDiff = [calendar components:unitFlags
fromDate:now
toDate:dateFromString
options:0];
NSLog("%@", componentsDayDiff);
这将产生以下结果:
<NSDateComponents: 0x75939f0>
Leap month: no
Day: 1
Hour: 1
Minute: 0
Second: 0
闰月:没有
日期:1
小时:1
分钟:0
秒:0
所以。。。它又出了什么问题?我无法理解您的逻辑,但如果您希望以您想要的方式获得差异,如
NSDateComponents
,您可以使用:
NSUInteger unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *componentsDaysDiff = [calendar components:unitFlags
fromDate:now
toDate:dateFromString
options:0];
NSLog("%@", componentsDayDiff);
这将产生以下结果:
<NSDateComponents: 0x75939f0>
Leap month: no
Day: 1
Hour: 1
Minute: 0
Second: 0
闰月:没有
日期:1
小时:1
分钟:0
秒:0
所以。。。它又出了什么问题?要获得两个NSDate对象之间的差异,请使用
timeintervalncedate:
。然后,要计算日、时、分和秒之间的间隔,请使用模除
NSTimeInterval doubleDiff = [date1 timeIntervalSinceDate:date2];
long diff = (long) doubleDiff;
int seconds = diff % 60;
diff = diff / 60;
int minutes = diff % 60;
diff = diff / 60;
int hours = diff % 24;
int days = diff / 24;
您可以通过不同的方式将NSTimeInterval(double)转换为long,具体取决于您是要向上取整、向下取整还是最近取整。要获得两个NSDate对象之间的差异,请使用
TimeIntervalsIncestate:
。然后,要计算日、时、分和秒之间的间隔,请使用模除
NSTimeInterval doubleDiff = [date1 timeIntervalSinceDate:date2];
long diff = (long) doubleDiff;
int seconds = diff % 60;
diff = diff / 60;
int minutes = diff % 60;
diff = diff / 60;
int hours = diff % 24;
int days = diff / 24;
您可以通过不同的方式将NSTimeInterval(double)转换为long。这取决于您是想向上取整、向下取整还是最近取整。您的问题在于,您是从“现在”开始计算小时、分钟和秒,而不是从日期/到日期。您的问题在于,您是从“现在”开始计算小时、分钟和秒,而不是从日期/到日期。这绝对是做到这一点的方法。这种方法的优点是,您可以通过需要更复杂的基于日历的计算的单位轻松获得时差。这没有问题,谢谢,这确实是一种方法。这种方法的优点是,您可以通过需要更复杂的日历计算的单位轻松获得时差。这没有问题。谢谢,它可以工作。是的,我知道这种解决方案,但我想使用日历组件function@Molwiko但是你知道你错在哪里了吗?您使用fromDate计算时/分/秒,但不使用toDate。然后在打印结果时做一些奇怪的算术。是的,我知道这个解决方案,但我想使用日历组件function@Molwiko但是你知道你错在哪里了吗?您使用fromDate计算时/分/秒,但不使用toDate。然后在打印结果时做一些奇怪的算术运算。