如何将24小时字符串时间转换为12小时字符串格式 [英] How to convert 24 hr String time into 12 hr time String format

查看:266
本文介绍了如何将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.

这篇关于如何将24小时字符串时间转换为12小时字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆