日期格式发生错误 [英] Date format gone wrong in swift

查看:125
本文介绍了日期格式发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置中等格式的日期,如: 2016年7月17日4:30:00 PM
,但我得到 07/17/2016 4:30:00 pm

I need to set a date in medium format like: Jul 17, 2016 4:30:00 PM, but I get 07/17/2016 4:30:00 p.m.

任何帮助都将非常感谢。提前致谢。
这是我的代码:

Any help will be highly appreciated. Thanks in advance. this is my code:

 let dateFormatter = NSDateFormatter()

 dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
 dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle

 let mydate = dateFormatter.stringFromDate(NSDate())


推荐答案

对于我来说,您正在使用的代码正是您需要的。

For me the code you are using dose exactly what you need.

如果您需要,可以使用 MMM dd,yyyy h:mm a 作为格式字符串你的格式化对象。代码如下:

In case you need you can achieve this by using MMM dd, yyyy h:mm a as format string for your formatter object. Code follows:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy h:mm a"
let mydate = dateFormatter.stringFromDate(NSDate())
// "Jul 17, 2016 11:48 AM"

这篇关于日期格式发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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