iphone: NSDateFormatter 如何以 24 小时格式显示时间?

标签 iphone objective-c ios nsdate nsdateformatter

NSDateFormatter *formatter1=[[NSDateFormatter alloc]init];
[formatter1 setDateFormat:@"HHmmss MMddyyyy"];
NSDate *finalDate =[formatter1 dateFromString:testTime];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[lblpickUpTime setText:[formatter stringFromDate:finalDate]];
[formatter release];
[formatter1 release];

其中 testTime = @"201518 07122011";我想要 24 小时内的结果

例如20.15 而不是晚上 8:15

有没有更好的方法来做到这一点?

最佳答案

通过设置尝试

[formatter setDateFormat:@"HH:mm"];  // replacement for [formatter setTimeStyle:NSDateFormatterShortStyle];

替代方法(不使用任何格式化程序):

NSRange range;
range.location = 2;
range.length = 2;
NSString *dateString = [NSString stringWithFormat:@"%@:%@",[myString substringToIndex:2],[myString substringWithRange:range]];

关于iphone: NSDateFormatter 如何以 24 小时格式显示时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7174411/

相关文章:

php - Apple Production Push Notification 证书在新应用版本后无法连接

iphone - 如何用空值重置 NSMutableString?

iphone - Cocoa 错误 256,当使用 initWithContentsOfURL :

ios - 如何在通过向上滑动杀死whatsapp后存档whatsapp(iOS版本)通知处理

iphone - NSMutableArrays

objective-c - 在桌面上显示进度栏(透明窗口) - Cocoa

objective-c - Objective-C 中的 C 对象

ios - 如果 UIButton 的大小是一定大小,如何更改它的大小

ios - 在 View 生命周期的什么时候,您可以通过 xib 加载 View 的最终帧?

ios - 具有基本身份验证挑战的 NSURLSession