2011-06-15 63 views
0

我想比较当前时间10:15的时间,然后找到差异即。时间为10点15分。一切编译,但我总是得到75 ....有人可以帮忙吗?如果可能,我会非常喜欢编码示例。此外,我试图使用 - (时间间隔)timeIntervalSinceNow,但我无法弄清楚如何工作,所以如果任何人也有这样的解释,我会非常感谢 *注意我使用的是版本3.2.6,请指教因此,谢谢如何正确找到此代码中的时差?

NSDate *today = [NSDate date]; 
NSCalendar *gregorian = [[NSCalendar alloc] 
            initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = 
[gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today]; 

NSInteger hour = [components hour]; 
NSInteger minute = [components minute]; 
NSInteger second = [components second]; 


NSInteger combo = hour + (minute/60) + (second/3600); 

NSTimeInterval time = combo*60*60; 

NSTimeInterval tenFifteenFirstPeriodStop = 10.25*60*60; 

    double myDouble; 
int myInt; 
NSString* myNewString; 
NSString *info; 



      if(time <tenFifteenFirstPeriodStop){ 
     myDouble = (tenFifteenFirstPeriodStop-time)/60; 
     myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5)); 
     myNewString = [NSString stringWithFormat:@"%d", myInt]; 
     info = [[NSString alloc] initWithFormat: 
     @"%@",myNewString]; 
     } 

      NSString *message = [[NSString alloc] initWithFormat: 
        @"%@", info]; 

    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Minutes Until the Bell   Rings" message:message delegate:nil cancelButtonTitle:@"Great!" otherButtonTitles:nil]; 
[alert show]; 
[alert release]; 
[message release]; 
+2

请尝试格式化您的代码 - 它现在看起来几乎不可读。 – 2011-06-15 15:34:32

回答

0

这会给你秒钟的时间,直到今天10.15。如果它已经通过,它将显示明天10.15秒的时间。

NSDate *today = [NSDate date]; 
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) 
              fromDate:today]; 
[components setMinute:15]; 
[components setHour:10]; 

NSDate * tenFifteenToday = [gregorian dateFromComponents:components]; 
if ([tenFifteenToday timeIntervalSinceNow] < 0) { 
    NSDateComponents * daysToAdd = [[[NSDateComponents alloc] init] autorelease]; 
    [daysToAdd setDay:1]; 

    NSDate * tenFifteenTomorrow = [gregorian dateByAddingComponents:daysToAdd toDate:tenFifteenToday options:0]; 
    NSLog(@"%.0lf", [tenFifteenTomorrow timeIntervalSinceNow]); 
} else { 
    NSLog(@"%.0lf", [tenFifteenToday timeIntervalSinceNow]); 
} 
+0

非常感谢你!我为我的节目编辑了一下,但是如果没有你的帮助,我无法完成它,再次感谢 - 凯莉 – 2011-06-15 20:28:08