如何格式化字符串变量中的数据 [英] How to format data from string variable
问题描述
我有一个保留日期的NSString:
I have a NSString that keeps date:
1900-01-01T11:00:00
我需要对其进行格式化,所以我有一个格式化程序:
I need to format it, so I have a formatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
如何立即设置此字符串的格式?
How to format this string now?
NSString* formatedDateString = [df ???];
推荐答案
创建日期格式化程序,该日期格式化程序返回给定字符串的日期对象,创建第二个日期格式化程序,从该日期返回所需格式的字符串
Create a date formatter, that returns a date object for for the given string, create a second date formatter, that returns a string in the desired format from that date.
NSDateformatter *inputFormatter = [[NSDateformatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *date = [inputFormatter dateFromString:dateString];
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSString *outputString = [outputFormatter stringFromDate:date];
但是您也可以让输出日期格式化程序根据地区来决定样式:
But you could also let the output date formatter decide the style in respect to the locale:
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setDateStyle:NSDateFormatterShortStyle];
[outputFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *outputString = [outputFormatter stringFromDate:date];
对于美国语言环境,您现在应该选择所需的格式.
for a american locale, you should now cat the desired format.
如果要为用户强制使用格式"dd/MM/yyyy hh:mm:ss a",则还应设置输出日期格式程序的区域设置.
if you want to enforce format "dd/MM/yyyy hh:mm:ss a" for the user, you should also set the locale of the output date formatter.
完整的命令行示例.
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *dateString1 = @"2012-12-31T11:00:00";
NSString *dateString2 = @"2012-12-31T14:00:00";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSDate *date1 = [inputFormatter dateFromString:dateString1];
NSDate *date2 = [inputFormatter dateFromString:dateString2];
NSString *outputString1 = [outputFormatter stringFromDate:date1];
NSString *outputString2 = [outputFormatter stringFromDate:date2];
NSLog(@"%@", outputString1);
NSLog(@"%@", outputString2);
}
return 0;
}
如果您不像那样设置语言环境,请执行以下操作:它不是碰巧是"en_US"
If you dont set the locale like [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
it will lead do a ugly mix of formats with user's sefault locale, if it not happend to be 'en_US'
即德语: 2012年12月31日02:00:00 nachm.
这是德语中未使用的格式.
ie in German: 31/12/2012 02:00:00 nachm.
This is a format not used in german.
要支持用户的语言和语言环境,您应该改用其他样式.
To support user's language and locale you should instead use the different styles.
提供的 q&a rmaddy表示,在极少数情况下,解析格式会被重写.为避免这种情况,请将输入格式的格式设置为特定的语言环境,例如 [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
The q&a rmaddy provided indicates, that there are rare cases, where the parse format gets rewritten. To avoid this, set the input formatted's format to a certain locale, like [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
这篇关于如何格式化字符串变量中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!