Date在设置系统地区为毛里求斯的,12小时制的时候为 NULL

吐槽:用户反馈回这个问题的时候,我用测试机测试,没问题,用自己的机子测试OK,(1)排除新机型带来的问题。

然后后台拿到用户数据,在项目上运行,能显示。(2)排除数据格式不正确,

开始模拟用户的数据,人在毛里求斯,设置语言法语,地区毛里求斯,没有问题(保持微笑)

后来同事提醒,试试 12小时制。。。。。(得,问题重现了)于是我开始漫长的求知之路(两天 ^>^)


以上结论:测试环境为地区设置为毛里求斯,12小时制。


源(原)代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, [UIScreen mainScreen].bounds.size.width/2,30)];
    label.textColor = [UIColor redColor];
    
    label.text = [self timeTransfer:@"20200427130101"];
    
    [self.view addSubview:label];
    
    NSLog(@"timeTransfer: %@",[self timeTransfer:@"20200427130101"]);

}

-(NSString *)timeTransfer:(NSString *) string{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
     [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
     
     NSDate *date = [dateFormatter dateFromString:string];
     
     [dateFormatter setDateFormat:@"dd MMM,yyyy HH:mm:ss"];    
      NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
      dateFormatter.locale = locale;

     NSString *strDate = [dateFormatter stringFromDate:date];
    
    return strDate;
}

就会发现拿到的数据为null

 

解决方法:
    
      NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
      dateFormatter.locale = locale;

放在

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

的下面

也就是

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, [UIScreen mainScreen].bounds.size.width/2,30)];
    label.textColor = [UIColor redColor];
    
    label.text = [self timeTransfer:@"20200427130101"];
    
    [self.view addSubview:label];
    
    NSLog(@"timeTransfer: %@",[self timeTransfer:@"20200427130101"]);

}

-(NSString *)timeTransfer:(NSString *) string{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    
      NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
      dateFormatter.locale = locale;
    
     [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
     
     NSDate *date = [dateFormatter dateFromString:string];
     
     [dateFormatter setDateFormat:@"dd MMM,yyyy HH:mm:ss"];

 
     NSString *strDate = [dateFormatter stringFromDate:date];
    
    return strDate;
}

鸣谢同事提醒,以及两位同仁的帮助~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值