我在我的viewController默认位置,UIDataPicker当我的用户完成选择日期运行此代码:NSDateFormatterLongStyle字符串的NSDate
NSString *dateString = [NSDateFormatter localizedStringFromDate:[self.dataPicker date]
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
与该代码我可以存储在以下格式的日期:
2016年5月31日
在我的代码之后,我需要这个字符串转换成真正的日期格式,为了这个,我用鳕鱼e如下:
-(NSDate*)convertStringToDate:(NSString*)date{
NSString *dateString = date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
return [dateFormatter dateFromString:dateString];
}
但是这段代码返回一个空值。由于日期选择器默认设置,我的系统可以接收任何日期格式,但最终我希望它被转换为en_us格式。
我该如何解决这个问题?
请参见本教程:HTTPS: //iosdevcenters.blogspot.com/2016/03/nsdateformatter-in-swift.html –
'2016年5月31日'vs'MM/dd/yyyy'您是否看到相同的格式?格式“MM/dd/yyyy”与字符串格式不匹配(2016年5月31日)。 – Larme
@Larme,我明白了...现在让我们将设备的语言更改为法语,格式将与2016年5月31日不同,我认为我需要将日期转换为语言环境... – Lacrifilm