开发者

How can i find the time difference in this code correctly?

I am trying to compare the current time to the time 10:15 and then find the difference ie. the minutes left until 10:15. E开发者_如何学Goverything compiles, but I always get 75....Can someone help?? I would really love coding examples if at all possible. Also I tried to use -(time interval)timeIntervalSinceNow, but I couldnt figure out how to work it, so if anyone has an explanation for that too, i would be extremely grateful *Note i am using Version 3.2.6, please advise accordingly, thanks

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];


This will give you the time in seconds till 10.15 today. If it has passed already, it will show the time in seconds to 10.15 tomorrow.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜