0

我试图从 NSString 转换为 NSdate 但它没有工作。请参阅我的代码。它的输出为“1991-01-04 18:30:00 +0000”。谁能帮我。提前致谢

NSString *selectedDateString =@"03/28/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/DD/YYYY"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate * selectedDate = [dateFormatter dateFromString:selectedDateString];
NSLog(@"selectedDate-->%@",selectedDate);
4

1 回答 1

0

如果这是您需要使用的唯一格式,我建议您使用dateStyleandtimeStyle而不是dateFormat将 dateStyle 设置为,NSDateFormatterStyleShort而将 timeStyle 设置为NSDateFormatterStyleNone. 这是防弹和本地化友好的:无论您的用户使用哪种语言环境,日期都将始终以正确的格式显示(对于用户)。

于 2013-06-14T14:39:44.837 回答