如何将24小时字符串时间转换为12小时字符串格式
问题描述:
我在NSString中有时间,例如"15:15"
I have time in NSString like "15:15"
我想将此NSString时间隐藏为12个小时的NSString(即"3:15 PM").
I want to covert this NSString time into 12 hr NSString(i.e "3:15 PM").
答
使用NSDateFormatter
将字符串转换为NSDate
.然后再次使用dateformatter将NSDate
更改为字符串.
Use NSDateFormatter
to turn the string in a NSDate
. Then use the dateformatter again to change the NSDate
to a string.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"HH:mm";
NSDate *date = [dateFormatter dateFromString:@"15:15"];
dateFormatter.dateFormat = @"hh:mm a";
NSString *pmamDateString = [dateFormatter stringFromDate:date];
代码可能包含一些错误,这些错误未经编译或测试就可以编写.
Code might contain some errors, written without compiling or testing.