ios 如何格式化用户语言环境的当前日期?

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

How do I format the current date for the user's locale?

iosxcode4

提问by SpokaneDude

I have this code where I'm trying to get the current date and format it in the current locale.

我有这段代码,我试图获取当前日期并在当前语言环境中对其进行格式化。

NSDate *now = [NSDate date];  //  gets current date
NSString *sNow = [[NSString alloc] initWithFormat:@"%@",now];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"mm-dd-yyyy"];
insertCmd = [insertCmd stringByAppendingString: formatter setDateFormat: @"MM.dd.yyyy"];

I know the last line is wrong, but can't seem to figure it out... "insertCmd" is a NSString that I'm building for a FMDB command.

我知道最后一行是错误的,但似乎无法弄清楚......“insertCmd”是我正在为 FMDB 命令构建的 NSString。

Help would be greatly appreciated, or a pointer to the "doc" where it's described.

帮助将不胜感激,或者指向描述它的“文档”。

回答by JiaYow

I wouldn't use setDateFormatin this case, because it restricts the date formatter to a specific date format (doh!) - you want a dynamic format depending on the user's locale.

setDateFormat在这种情况下我不会使用,因为它将日期格式化程序限制为特定的日期格式(doh!) - 您需要根据用户的区域设置的动态格式。

NSDateFormatter provides you with a set of built-in date/time stylesthat you can choose from, i.e. NSDateFormatterMediumStyle, NSDateFormatterShortStyle and so on.

NSDateFormatter 为您提供了一组内置的日期/时间样式供您选择,即 NSDateFormatterMediumStyle、NSDateFormatterShortStyle 等。

So what you should do is:

所以你应该做的是:

NSDate* now = [NSDate date];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
NSString* myString = [df stringFromDate:now];

This will provide you with a string with a medium-length date and a short-length time, all depending on the user's locale. Experiment with the settings and choose whichever you like.

这将为您提供具有中等长度日期和较短时间的字符串,所有这些都取决于用户的语言环境。尝试设置并选择您喜欢的任何设置。

Here's a list of available styles: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/c/tdef/NSDateFormatterStyle

以下是可用样式列表:https: //developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/c/tdef/NSDateFormatterStyle

回答by Yuri Romanchenko

In addition to jiayowanswer you can specify your custom 'template' to get localized version:

除了jiayow答案,您还可以指定自定义“模板”以获取本地化版本:

+ (NSString *)formattedDate:(NSDate *)date usingTemplate:(NSString *)template {
    NSDateFormatter* formatter = [NSDateFormatter new];

    formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:template options:0 locale:formatter.locale];

    return [formatter stringFromDate:date];
}

Example usage for US/DE locale:

US/DE 语言环境的示例用法:

NSLocale *enLocale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
NSLocale *deLocale = [NSLocale localeWithLocaleIdentifier:@"de"];

// en_US:   MMM dd, yyyy
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddMMMyyyy" options:0 locale:enLocale];
// de:      dd. MMM yyyy
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddMMMyyyy" options:0 locale:deLocale];

// en_US:   MM/dd/yyyy
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddyyyyMM" options:0 locale:enLocale];
// de:      dd.MM.yyyy
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddyyyyMM" options:0 locale:deLocale];

// en_US    MM/dd
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MMdd" options:0 locale:enLocale];
// de:      dd.MM.
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MMdd" options:0 locale:deLocale];

回答by algrid

Here's a Swift 5 version of @JiaYou's answer:

这是@JiaYou 答案的 Swift 5 版本:

func format(date: Date) -> String {
    let formatter = DateFormatter()
    formatter.dateStyle = .medium
    formatter.timeStyle = .none
    return formatter.string(from: date)
}

Here I need date only, so I indicate the timeStyleas none.

在这里我只需要日期,所以我将timeStyleas表示为none

回答by Adam Freeman

If you want the localized date and time this will give it to you:

如果你想要本地化的日期和时间,这会给你:

    NSString *localizedDateTime = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];

The code above does not give you the localized date and time.

上面的代码没有给你本地化的日期和时间。