当我从xcode在模拟器中调试时,此NSDate能够显示节目。但是当我在iphone6中调试时,它显示为NULL。
NSString *dateWithTime = [NSString stringWithFormat:@"2019/03/18 09:30 AM"];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd hh:mm a";
[fmt setAMSymbol:@"am"];
[fmt setPMSymbol:@"pm"];
fmt.timeZone = [NSTimeZone systemTimeZone];
NSDate *localDate = [fmt dateFromString:dateWithTime];
NSLog(@"DATE -- %@ ", [fmt stringFromDate:localDate]);
不知道这是因为电话时区未使用am / pm或任何其他原因。
请帮助我回答。
答案 0 :(得分:2)
尝试设置日期格式器的区域设置
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
更新
对于en_US_POSIX语言环境,您可以使用:
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];