In My application I have to complete a particular task in given time.So first i calculated the time complete the task in seconds and then add that time to the current that like this.
NSDate *mydate = [NSDate date];
NSTimeInterval TotalDuraionInSec = sec.cal_time * 60;
TaskCmpltTime = [mydate addTimeInterval:TotalDuraionInSec];
NSLog(@"task will be completed at%@",TaskCmpltTime);
now I compare time like this
if([CurrentTime isEqualToDate:AfterCmpltTime]){
NSLog (@"Time Finish");
}
but I want to know is Time is left or not.Is current time is less then or greater then current time how can i know this ?
I have an example where I get the time from a picker and check if its today or tomorrow. You should be able to just take the code and use it in your way...
Yeah, for your purposes it's probably best to work in time intervals. The NSTimeInterval in Objective-C is an alias for
double
, and it represents a time value in seconds (and, of course, fractions, down to at least millisecond resolution).There are several methods on NSDate for this --
+timeIntervalSinceReferenceDate
, which returns the number of seconds since Jan 1, 2001,-timeIntervalSinceReferenceDate
, which returns the difference in time between the supplied NSDate object and Jan 1, 2001,-timeIntervalSinceDate:
, which returns the difference in seconds between the two NSDate objects, and-timeIntervalSinceNow
, which returns the difference between the current time and the NSDate object.Lots of times it's most convenient to store an NSDate value as an NSTimeInterval instead (eg, timeIntervalSinceReferenceDate). This way it doesn't have to be retained and disposed, etc.
timeIntervalSinceNow compares the NSDate with Now. if NSDate is after Now the return value is possitive, if the date is earlier than Now the result is negative.