倒数计时器实现的两个日期之间的差异 [英] Difference between two dates for countdown timer implementation
问题描述
两个日期之间的差错怎么可能是错误的(2013-04-04T19:14:58 + 02:00-2013-04-03T18:14:58 + 02:00)?
How is possible that the difference between two dates is wrong (2013-04-04T19:14:58+02:00-2013-04-03T18:14:58+02:00) ?
代码的结果是
01天06时46分02秒
01 days 06 hours 46minutes 02 seconds
结果完全错误,正确答案应该是
the result is totally wrong the right answer should be
01天01小时00分钟00秒
01 days 01 hours 00 minutes 00 seconds
您能否帮助我找出问题的根源?
Can you pls help me to figure out the source of the issue ?
代码:
-(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函数:
SqldateStringTodate Function :
+(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
的不同之处,则可以使用:
I could not understand your logic, but if you want to get the difference as NSDateComponents
in the way you want, you can use:
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
所以...又怎么了?
这篇关于倒数计时器实现的两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!