在手机中进行调试时,NSDate返回NULL

时间:2019-03-12 03:10:54

标签: ios objective-c nsdateformatter

当我从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或任何其他原因。

请帮助我回答。

1 个答案:

答案 0 :(得分:2)

尝试设置日期格式器的区域设置

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

更新

对于en_US_POSIX语言环境,您可以使用:

 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];