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.
非常感谢帮助,或指向描述它的“doc”的指针。
3 个解决方案
#1
30
I wouldn't use setDateFormat
in 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 styles that 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
#2
5
In addition to jiayow answer 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];
#3
3
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.
上面的代码没有为您提供本地化的日期和时间。
#1
30
I wouldn't use setDateFormat
in 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 styles that 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
#2
5
In addition to jiayow answer 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];
#3
3
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.
上面的代码没有为您提供本地化的日期和时间。