how to compare time with current date and time?

人走茶凉 提交于 2020-01-06 08:58:07

问题


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 ?


回答1:


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");
}



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/7370123/how-to-compare-time-with-current-date-and-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!