日期总是为零,但是如果我将dateString更改为03/01/1975之类的任何其他日期,它会正常工作。有什么想法吗?
NSString *dateString = @"05/01/1975";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"us"];
[dateFormatter setLocale:gbLocale];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
答案 0 :(得分:1)
试试这段代码
- (NSDate *)getPickerDateForString:(NSString *)dateString
{
NSDate * dt;
if (!dateFormatter)
{
dateFormatter = [[NSDateFormatter alloc] init];
}
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
dt = [dateFormatter dateFromString:dateString];
return dt;
}
答案 1 :(得分:1)
试试这个
NSString *dateString = @"05/01/1975";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[formatter setDateFormat:@"MM.dd.yyyy"];
NSDate *anotherDate = [formatter dateFromString:dateString];
NSLog(@"%@",anotherDate);
它会提供这样的输出
1975-05-01 00:00:00 +0000