如何将24小时时间格式转换为12小时时间格式? [英] How can I convert the 24 hour time format to 12 hour time format?

查看:174
本文介绍了如何将24小时时间格式转换为12小时时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以获得24小时的时间,但如何获得上午和下午的12小时格式?

I can get the 24 hour style time, but how do I get the 12 hour format with the am and pm?

let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Hour, .Minute, .Second], fromDate: date)
let hour = components.hour
let minutes = components.minute


推荐答案

如你所说,你应该使用 NSDateFormatter 为你的期望的格式。希望这对你有帮助。

As you say, you should use NSDateFormatter for your desired format. Hope this will help you.

let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "hh:mm a"
let time = formatter.stringFromDate(date)

有关日期格式化程序的详细信息,请参阅 NSDateFormatter

For more information about date formatter see NSDateFormatter

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

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