更改24小时后,dateFromString返回nil

编程入门 行业动态 更新时间:2024-10-25 16:19:50
本文介绍了更改24小时后,dateFromString返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@"hh:mm a"]; NSDate* sourceDate = [timeFormat dateFromString:@"19/11/2010 12:00 am"]; if(sourceDate==nil) { NSLog(@"source date nil"); }

我在iPhone中使用24小时设置。更改为24小时设置后,datefromstring始终返回nil。我改为12小时格式,它再次运行。为什么?

I'm using 24 hour setting in iPhone. After I change to 24 hour setting, datefromstring always return nil. I rechange to 12 hour format, it's working again. Why ?

推荐答案

需要设置区域设置以避免受到24小时设置更改的影响。 格式还必须匹配输入字符串(在您的示例中包含日期):

The locale needs to be set to avoid being affected by the 24-hour setting change. The format also has to match the input string (which in your example includes the date):

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; [timeFormat setLocale:locale]; [timeFormat setDateFormat:@"dd/MM/yyyy hh:mm a"]; NSDate* sourceDate = [timeFormat dateFromString:@"19/11/2010 12:00 am"]; if(sourceDate==nil) { NSLog(@"source date nil"); }

参见 QA1480 了解更多信息。

更多推荐

更改24小时后,dateFromString返回nil

本文发布于:2023-11-27 01:53:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小时后   nil   dateFromString

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!