OGeek|极客世界-中国程序员成长平台

标题: ios - 从 12 小时格式到 24 小时格式的时间转换 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:11
标题: ios - 从 12 小时格式到 24 小时格式的时间转换

我编写了以下代码将时间从 12 小时格式转换为 24 小时格式。它适用于模拟器,但在使用设备时它返回 null。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat =@"hh:mm a";
NSString * endTimeString=[NSString stringWithFormat"%@",[[tempArray objectAtIndex:i] objectForKey"EndTime"]];
NSLog(@"Endtime from server responce:%@",endTimeString);
NSDate *date = [dateFormatter dateFromString:endTimeString];
dateFormatter.dateFormat = @"HH:mm" ;
NSString *pmamDateString = [dateFormatter stringFromDate:date];
NSLog(@"Converted EndTime:%@ ",pmamDateString);



Best Answer-推荐答案


尝试在您的 NSDateFormatter 上设置语言环境:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier"en_US_POSIX"];
[dateFormatter setLocale:locale];

关于ios - 从 12 小时格式到 24 小时格式的时间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32842879/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4