Ios 比较当前日期和时间 NSDate*currentTime=[NSDate-date]; NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init]; [日期格式化程序setDateFormat:@“hh:mm”]; NSString*resultString=[dateFormatter stringFromDate:currentTime]; NSDate*firstDate=[dateFormatter dateFromString:resultString]; NSDate*secondDate=[dateFormatter dateFromString:@“16:00”]; NSTimeInterval时差=[secondDate TimeIntervalsIncestDate:firstDate]; 如果(时差

Ios 比较当前日期和时间 NSDate*currentTime=[NSDate-date]; NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init]; [日期格式化程序setDateFormat:@“hh:mm”]; NSString*resultString=[dateFormatter stringFromDate:currentTime]; NSDate*firstDate=[dateFormatter dateFromString:resultString]; NSDate*secondDate=[dateFormatter dateFromString:@“16:00”]; NSTimeInterval时差=[secondDate TimeIntervalsIncestDate:firstDate]; 如果(时差,ios,objective-c,datetime,Ios,Objective C,Datetime,我正在尝试检索当前时间并将其与输入时间进行比较。这是我目前代码的一部分(iOS的目标c),但NSTimeInterval会导致断点。除此之外,我还希望将日期合并到比较中,因此,如果是在周二下午5:00之后,将发生事件。因此,如果您对如何合并该事件有任何想法,将不胜感激!比较两个日期: NSDate *currentTime = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

我正在尝试检索当前时间并将其与输入时间进行比较。这是我目前代码的一部分(iOS的目标c),但NSTimeInterval会导致断点。除此之外,我还希望将日期合并到比较中,因此,如果是在周二下午5:00之后,将发生事件。因此,如果您对如何合并该事件有任何想法,将不胜感激!

比较两个日期:

NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                         [dateFormatter setDateFormat:@"hh:mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
NSDate* firstDate = [dateFormatter dateFromString:resultString];
NSDate* secondDate = [dateFormatter dateFromString:@"16:00"];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];

if (timeDifference < 0){
这将解决您的问题。

要比较两个日期:

NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                         [dateFormatter setDateFormat:@"hh:mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
NSDate* firstDate = [dateFormatter dateFromString:resultString];
NSDate* secondDate = [dateFormatter dateFromString:@"16:00"];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];

if (timeDifference < 0){
if ([firstDate compare:secondDate] == NSOrderedDescending) {
    NSLog(@"firstDate is later than secondDate");
    NSTimeInterval timeDifference = [firstDate timeIntervalSinceDate:secondDate];
} else if ([firstDate compare:secondDate] == NSOrderedAscending) {
    NSLog(@"firstDate is earlier than secondDate");
    NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
} else {
    NSLog(@"firstDate and secondDate are the same");
}

这应该可以解决您的问题。

John下面的答案是比较日期的基本形式。(注意,在比较之前不需要转换为字符串并再次返回。)关于“周二下午5点后”的那句话有点鬼鬼祟祟,可能需要单独问一个问题——需要指定不同时区的行为,是否“any”周二,Etchjohn下面的答案是比较日期的基本形式。(注意,在比较之前不需要转换成字符串并再次返回。)关于“周二下午5点后”的那句话有点狡猾,可能需要另外一个问题——需要指定不同时区的行为,是否“有”“星期二,等等,如果我希望它只在某一天发生,那该怎么办?比如说,如果它是星期二的5:00,那么会发生一件事,但是我们在5:00结婚,它不会发生。@DSmith:a“date”如NSDate对象所体现的,此类对象表示绝对全局意义上的真实世界时间中的特定单点。该时间点可以呈现为特定时区的日期和时间,或与其他特定时间点进行比较。它不表示一般的“一天中的时间”或者任何其他一般概念。如果您想知道如何表示您正在寻找的内容,您可能需要更具体地问这个问题,以便为建模提供最佳帮助。如果我只想让它在某一天发生呢?比如说,如果周二5:00会发生事件,但我们在5:00不会发生。@DSmith:a“日期”如NSDate对象所体现的,此类对象表示绝对全局意义上的真实世界时间中的特定单点。该时间点可以呈现为特定时区的日期和时间,或与其他特定时间点进行比较。它不表示一般的“一天中的时间”或者任何其他一般概念。如果您想知道如何表示您正在寻找的内容,您可能希望更具体地问这个问题,以获得建模的最佳帮助。
if ([firstDate compare:secondDate] == NSOrderedDescending) {
    NSLog(@"firstDate is later than secondDate");
    NSTimeInterval timeDifference = [firstDate timeIntervalSinceDate:secondDate];
} else if ([firstDate compare:secondDate] == NSOrderedAscending) {
    NSLog(@"firstDate is earlier than secondDate");
    NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
} else {
    NSLog(@"firstDate and secondDate are the same");
}