简体   繁体   中英

how to compare time with current date and time?

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 ?

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.

double timeLeft = [TaskCompltTime timeIntervalSinceNow];

 if(  timeLeft > 0.0 )
 // still time left 

 else
     //time is up

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...

int selectedHour =  [customPickerView selectedRowInComponent:0];
int selectedMinute =  [customPickerView selectedRowInComponent:1];

NSDate *today = [NSDate date];
NSDateFormatter *weekdayFormatter = [[[NSDateFormatter alloc] init]autorelease];
NSDateFormatter *hmformatter = [[[NSDateFormatter alloc] init]autorelease];
[hmformatter setDateFormat: @"hh mm"];
[weekdayFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[weekdayFormatter setDateFormat: @"EE"];
// NSString *formattedDate = [formatter stringFromDate: today];


NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
NSDateComponents *dateComponentsToday = [gregorian components:(NSHourCalendarUnit  | NSMinuteCalendarUnit | NSDayCalendarUnit) fromDate:today];


NSInteger currentHour = [dateComponentsToday hour];
NSInteger currentMinute = [dateComponentsToday minute];

NSString *weekday; 



if ((selectedHour > currentHour) | ((selectedHour == currentHour) & (selectedMinute > currentMinute))) {
    //so we are still in today
    weekday = [weekdayFormatter stringFromDate: today];
    weekday =  NSLocalizedString(@"today", @"today");
} else {
    //the timer should start tomorrow
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *tomorrow = [today dateByAddingTimeInterval:secondsPerDay];
    weekday = [weekdayFormatter stringFromDate: tomorrow];
    weekday =  NSLocalizedString(@"tomorrow", @"tomorrow");
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM