Ios 在Objective-c中检查时间?

Ios 在Objective-c中检查时间?,ios,objective-c,datetime,time,Ios,Objective C,Datetime,Time,如何以hh:mm格式获取当前时间?我需要能够区分上午和下午,并比较当前时间和第二次。我确信这是一个愚蠢的函数,但我似乎无法理解它。当前日期和日期比较: NSDate * now = [NSDate date]; NSDate * mile = [[NSDate alloc] initWithString:@"2001-03-24 10:45:32 +0600"]; NSComparisonResult result = [now compare:mile]; NSLog(@"%@", now)

如何以hh:mm格式获取当前时间?我需要能够区分上午和下午,并比较当前时间和第二次。我确信这是一个愚蠢的函数,但我似乎无法理解它。

当前日期和日期比较:

NSDate * now = [NSDate date];
NSDate * mile = [[NSDate alloc] initWithString:@"2001-03-24 10:45:32 +0600"];
NSComparisonResult result = [now compare:mile];

NSLog(@"%@", now);
NSLog(@"%@", mile);

switch (result)
{
    case NSOrderedAscending: NSLog(@"%@ is in future from %@", mile, now); break;
    case NSOrderedDescending: NSLog(@"%@ is in past from %@", mile, now); break;
    case NSOrderedSame: NSLog(@"%@ is the same as %@", mile, now); break;
    default: NSLog(@"erorr dates %@, %@", mile, now); break;
}

[mile release];
对于日期格式,有一个
NSDateFormatter
。您可以在《数据格式指南》和中找到更多信息,例如链接:

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];

NSDate *formatterDate = [inputFormatter dateFromString:@"1999-07-11 at 10:30"];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"HH:mm 'on' EEEE MMMM d"];

NSString *newDateString = [outputFormatter stringFromDate:formatterDate];

NSLog(@"newDateString %@", newDateString);
// For US English, the output is:
// newDateString 10:30 on Sunday July 11

接下来的几天我都在看医生,但我会检查一下,然后再给你回复。顺便说一句,我不需要日期代码,只需要时间。
NSDate
类表示Cocoa中的日期和时间,我刚刚复制了我的两个示例,您应该能够找到如何更新格式字符串以获得时间。。。