ios 检测时间格式是 12 小时还是 24 小时格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7448360/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:33:28  来源:igfitidea点击:

Detect if time format is in 12hr or 24hr format

objective-cioscocoa-touchnstimezone

提问by JonasG

Is there any way to detect if the current device of the app uses 12h our 24h format, so that I can use one NSDateFormatter for 12h and one for 24h depending on the users language/loaction setting? Just Like the UIDatePicker detects and shows the AM/PM picker if it is 12h format.

有什么方法可以检测应用程序的当前设备是否使用 12 小时我们的 24 小时格式,以便我可以根据用户的语言/位置设置使用一个 NSDateFormatter 12 小时和一个 24 小时?就像 UIDatePicker 检测并显示 AM/PM 选择器一样,如果它是 12h 格式。

回答by JonasG

I figured it out, its pretty easy. I just added this code to viewDidLoad:

我想通了,这很容易。我刚刚将此代码添加到viewDidLoad

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
[formatter release];
NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));

And it perfectly returns YESor NOdepending on the locale.

它完美地返回YESNO取决于语言环境。

回答by Mostafa Berg

And here is a Swift 3.0 updated version

这是一个 Swift 3.0 更新版本

func using12hClockFormat() -> Bool {

    let formatter = DateFormatter()
    formatter.locale = Locale.current
    formatter.dateStyle = .none
    formatter.timeStyle = .short

    let dateString = formatter.string(from: Date())
    let amRange = dateString.range(of: formatter.amSymbol)
    let pmRange = dateString.range(of: formatter.pmSymbol)

    return !(pmRange == nil && amRange == nil)
}

回答by beowulf

this is swift solution that worked for me, those two above did not.

这是对我有用的快速解决方案,上面的两个没有。

let dateString : String = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current)!

if(dateString.contains("a")){ // 12 h format return true }else{ // 24 h format return false }

回答by Wiingaard

Here is the Swift version:

这是 Swift 版本:

func using12hClockFormat() -> Bool {

    let formatter = NSDateFormatter()
    formatter.locale = NSLocale.currentLocale()
    formatter.dateStyle = NSDateFormatterStyle.NoStyle
    formatter.timeStyle = NSDateFormatterStyle.ShortStyle

    let dateString = formatter.stringFromDate(NSDate())
    let amRange = dateString.rangeOfString(formatter.AMSymbol)
    let pmRange = dateString.rangeOfString(formatter.PMSymbol)

    return !(pmRange == nil && amRange == nil)
}

回答by Denis Kutlubaev

Objective C category NSDate+Extensions:

目标 C 类别NSDate+Extensions

@import Foundation;

@interface NSDate (Extensions)

- (NSString *)getTimeString;

@end


#import "NSDate+Extensions.h"

@implementation NSDate (Extensions)

- (NSString *)getTimeString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if ([self isTwelveHourDateFormat]) {
        [formatter setDateFormat:@"hh:mm\ndd MMM"];
    }
    else {
        [formatter setDateFormat:@"HH:mm\ndd MMM"];
    }
    return [formatter stringFromDate:self];
}

- (BOOL)isTwelveHourDateFormat
{
    NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
    return [dateFormat containsString:@"a"];
}

@end