如何以 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
。您可以在 Date and Time programming guide for cocoa 中找到更多信息和 Date formatters在数据格式指南中,来自链接的示例:
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
关于ios - 在 Objective-c 中检查时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2576130/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |