xcode 从用户在 iOS 上设置的时间获取 AM / PM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10079328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get AM / PM from users set time on iOS
提问by Neil
I am wondering if there is a way that you can get am/pm from the users set time on iOS.
我想知道是否有一种方法可以从用户在 iOS 上设置的时间获取 am/pm。
I am currently using this method to get time:
我目前正在使用这种方法来获取时间:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH"];
hours.text = [formatter stringFromDate:[NSDate date]];
[formatter setDateFormat:@"m"];
minutes.text = [formatter stringFromDate:[NSDate date]];
if ([minutes.text intValue] < 10) {
minutes.text = [NSString stringWithFormat:@"0%@",[formatter stringFromDate:[NSDate date]]];
}
[formatter release];
I know I am using HH to get the hours and for the 12 hour format I have to use "hh" bu this does not solve the problem, I need to retrieve the AM/PM.
我知道我使用 HH 来获取小时数,对于 12 小时格式,我必须使用“hh”,但这并不能解决问题,我需要检索 AM/PM。
Hopefully someone can help!
希望有人能帮忙!
回答by PRNDL Development Studios
Here you go. This is the date with am/pm formatting.
干得好。这是带有 am/pm 格式的日期。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss a"];
NSLog(@"Today's Date and Time: %@", [formatter stringFromDate:[NSDate date]]);
[formatter release];
回答by Bart?omiej Semańczyk
The simplest answer with Swift:
Swift 最简单的答案:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "a"
let currentAMPMFormat = dateFormatter.stringFromDate(NSDate()).uppercaseString