ios NSDate,比较两个日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9781467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSDate, comparing two dates
提问by Mike Owens
Ok, I have been working on this for a couple of days now and am stuck. What I am trying to do is compare the current time with a predetermined time. What I want to happen is, Everyday at certain times I want certain code to fire, but not before the time of day that I specify.
好的,我已经为此工作了几天,但被卡住了。我想要做的是将当前时间与预定时间进行比较。我想要发生的是,每天在特定时间我都希望触发某些代码,但不是在我指定的时间之前。
so, basically if the "current time" is the same or equal to "predetermined time" then fire this code. Or fire this line of code.
因此,基本上如果“当前时间”等于或等于“预定时间”,则触发此代码。或者触发这行代码。
Sounds simple, but I am having trouble grabbing comparing the two times.
听起来很简单,但我很难抓住两次比较。
I have formatted a string to a date like so
我已将字符串格式化为这样的日期
NSString* dateString = @"11:05 PM";
NSDateFormatter* firstDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[firstDateFormatter setDateFormat:@"h:mm a"];
[firstDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate* date = [firstDateFormatter dateFromString:dateString];
NSLog(@"date %@",date);
then i grab the current time like so
然后我像这样抓住当前时间
NSDate *currentTime = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone] secondsFromGMT]];
NSDateComponents* comps = [[NSCalendar currentCalendar]
components: NSHourCalendarUnit |NSMinuteCalendarUnit
|NSSecondCalendarUnit fromDate:currentTime];
[[NSCalendar currentCalendar] dateFromComponents:comps];
SO how do I compare these two times? I have tried everything I can think of, please help.
那么我如何比较这两次?我已经尝试了所有我能想到的,请帮助。
回答by Raj
Can you try below lines of code?
你可以试试下面的代码行吗?
switch ([dateOne compare:dateTwo]){
case NSOrderedAscending:
NSLog(@"NSOrderedAscending");
break;
case NSOrderedSame:
NSLog(@"NSOrderedSame");
break;
case NSOrderedDescending:
NSLog(@"NSOrderedDescending");
break;
}
But in your case I think you need to keep both dates in same format ..or some thing like this
但在你的情况下,我认为你需要将两个日期保持在相同的格式......或者像这样的东西
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
[components setHour:23];//change your time to 24hrs formate
[components setMinute:05];
[components setSecond:00];
NSDate *dateOne = [[NSCalendar currentCalendar] dateFromComponents:components];
components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSDate *dateTwo = [[NSCalendar currentCalendar] dateFromComponents:components];
and do comparison between dateOne and dateTwo.
并在 dateOne 和 dateTwo 之间进行比较。
回答by nithin
Try this condition:
试试这个条件:
if ([date compare:currentTime]==NSOrderedSame) {
//do want you want
}
回答by Madhu
Assuming your issue is actually comparing the two NSDate objects, try checking out the question linked below; it has some good pointers;
假设您的问题实际上是比较两个 NSDate 对象,请尝试查看下面链接的问题;它有一些很好的指针;