问题
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) ?
The result of the code is
01 days 06 hours 46minutes 02 seconds
the result is totally wrong the right answer should be
01 days 01 hours 00 minutes 00 seconds
Can you pls help me to figure out the source of the issue ?
The Code :
-(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 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;
}
回答1:
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);
This yield results:
<NSDateComponents: 0x75939f0>
Leap month: no
Day: 1
Hour: 1
Minute: 0
Second: 0
So... What's wrong with it again?
回答2:
To get the difference between two NSDate objects use timeIntervalSinceDate:
. Then, to compute days, hours, minutes, and seconds between, use modulo division.
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;
You can fudge the conversion from NSTimeInterval (double) to long different ways, depending on whether you want to round up, down, or to closest.
来源:https://stackoverflow.com/questions/15792640/difference-between-two-dates-for-countdown-timer-implementation