iphone——两次比较

iphone——两次比较,iphone,Iphone,我如何在HH:mm:ss格式中给出两次,然后比较并确定哪一次更晚 12:00:00和14:00:00 如何确定14:00:00是将输入字符串转换为NSDateFormatter(initWithDateFormat:allownaturallalanguage:,dateWithString)实例的较晚时间。然后使用像earlierDate:或compare:这样的方法对生成的NSDates.查看NSDateFormatter(initWithDateFormat:allownaturallal

我如何在HH:mm:ss格式中给出两次,然后比较并确定哪一次更晚

12:00:00和14:00:00


如何确定14:00:00是将输入字符串转换为
NSDateFormatter
initWithDateFormat:allownaturallalanguage:
dateWithString
)实例的较晚时间。然后使用像
earlierDate:
compare:
这样的方法对生成的
NSDate
s.

查看
NSDateFormatter
initWithDateFormat:allownaturallalanguage:
dateWithString
)将输入字符串转换为
NSDate
实例。然后在生成的
NSDate
s上使用
earlierDate:
compare:
等方法。

如果您在相同的时区内工作(是的,这会增加复杂性),那么通常最容易将这些字符串转换为NSDate对象,然后直接比较它们。有关NSDate和NSCalendar对象之间的转换,请参阅


如果您在相同的时区内工作(是的,这增加了复杂性),那么将这些字符串转换为NSDate对象并直接进行比较通常是最容易的。有关NSDate和NSCalendar对象之间的转换,请参阅


在前面的答案中,还有一个关于“如何做”的SO问题,如果您有两个带有@“12:00:00”和@“14:00:00”的NSstring,您可以忽略:

NSString *timeA, *timeB; // <-- these are your two times in the format 
    // HH:mm:ss where HH is 24h format (no am/pm) and midnight is 00 
    // and you always have two digits (i.e. 12:3:50 for 3m50s past 12 is 
    // always shown as 12:03:50) ...
BOOL timeAIsSooner = [[[timeA componentsSeparatedByString:@":"] 
        componentsJoinedByString:@""] intValue]
    < [[[timeB componentsSeparatedByString:@":"] 
        componentsJoinedByString:@""] intValue];

NSString*timeA,*timeB;// 为了补充前面的答案,如果您实际上有两个带有@“12:00:00”和@“14:00:00”的NSstring,您可以忽略:

NSString *timeA, *timeB; // <-- these are your two times in the format 
    // HH:mm:ss where HH is 24h format (no am/pm) and midnight is 00 
    // and you always have two digits (i.e. 12:3:50 for 3m50s past 12 is 
    // always shown as 12:03:50) ...
BOOL timeAIsSooner = [[[timeA componentsSeparatedByString:@":"] 
        componentsJoinedByString:@""] intValue]
    < [[[timeB componentsSeparatedByString:@":"] 
        componentsJoinedByString:@""] intValue];
NSString*timeA,*timeB//