gpt4 book ai didi

iphone - 将NSStrings转换为NSDates时如何避免语言问题?

转载 作者:行者123 更新时间:2023-12-01 17:46:03 25 4
gpt4 key购买 nike

我正在尝试将NSString转换为NSDate。如果将iPhone区域设置为英语(美国),则可以正常使用,但是当我将其设置为瑞典语时,则不能。

我的代码:

[...]    
// Get the date from the post
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZ"];

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

NSDate *dateFromString = [[[NSDate alloc] init] retain];
dateFromString = [dateFormatter dateFromString:[[stories objectAtIndex:storyIndex] objectForKey: @"date"]];

NSLog(@"String: %@", [[stories objectAtIndex:storyIndex] objectForKey: @"date"]);
NSLog(@"date From string: %@", dateFromString);

// Set date string
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:MM"];
NSString *stringFromDate = [formatter stringFromDate:dateFromString];
stringFromDate = [stringFromDate stringByReplacingOccurrencesOfString:@"\n" withString:@""];

NSLog(@"StringDate: %@", [dateFormatter stringFromDate:[[NSDate alloc] init]]);
[...]

记录结果:
[...]
2009-11-27 16:13:22.804 sportal.se[755:4803] String: Fri, 27 Nov 2009 12:56:13 +0100


2009-11-27 16:13:22.812 sportal.se[755:4803] date From string: (null)
2009-11-27 16:13:22.819 sportal.se[755:4803] StringDate: fre, 27 nov 2009 16:13:22 +0100
[...]

这里的问题在于,它期望的是“fre,2009年11月27日... + 0100”,但它的值为“Fri,2009年11月27日... + 0100”。我怎样才能解决这个问题?

最佳答案

NSDateFormatter具有属性locale。尝试:

dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]
autorelease];

关于iphone - 将NSStrings转换为NSDates时如何避免语言问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1809379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com