iphone - 当前时间在 HH :MM:SS am/pm format?

标签 iphone objective-c

如何获取 HH:MM:SS am/pm 格式的当前时间?

最佳答案

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss a"];
NSLog(@"Current Date: %@", [formatter stringFromDate:[NSDate date]]);
[formatter release];

setDateFormat中的字符串格式基于ISO-8601,http://en.wikipedia.org/wiki/ISO_8601#Dates

Swift 3 版本:

let formatter = DateFormatter()
formatter.dateFormat = "hh:mm:ss a"
print("Current date: \(formatter.string(from: Date()))") // -> Current date: 08:48:48 PM

还有一个新网站,因为这个原始答案有助于编写这些格式字符串。恰如其分地命名... http://nsdateformatter.com

关于iphone - 当前时间在 HH :MM:SS am/pm format?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4715591/

相关文章:

ios - 自定义Segue正确方法

iphone - 使用 AVFoundation 逐帧录制音频和视频

objective-c - 将用户输入发送到 url 并获取结果并将其显示在 cocoa Objective C 的程序中

ios - 设备旋转时如何为 View 设置正确的边界?

objective-c - 整个应用程序中的自定义后退按钮 UINavigationController

ios - UIRefreshControl 选择器不调用 objective-c

iphone - iPhone 和服务器之间的安全通信?

iphone - IOS 应用 UIWebView 或导航 Controller

ios - Xcode5错误: Warning attempt to present <View> on <OtherView> while presentation is in progress

iphone - 为什么 Xcode 5 不允许我为 iPhone 应用程序指定 iPad 图像? iTunes Artwork 图标呢?