Swift将NSDate转换为24h格式在Device上失败 [英] Swift converting NSDate to 24h Format fails on Device

查看:134
本文介绍了Swift将NSDate转换为24h格式在Device上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在iPhone6上运行我的应用程序时,我遇到了一个恼人的错误。

我需要将一小时(由datePicker选择)从12h转换为24h格式。

简单的事!是的,我很确定,但......我的代码在模拟器上工作,但在设备上没有。

代码如下:

I'm facing an annoying error when i run my application on my iPhone6.
I need to convert an hour (picked by a datePicker) from 12h to 24h format.
Simple thing! Right, I'm pretty sure about that but... my code works on simulator but not on device.
The code is the following:

func convertTimeTo24HoursFomratString() -> String {
        let dateFormatter = NSDateFormatter()
        // isFormatTime24Hours is a static function that returns me if the device has a 24h clock or not. It works fine both on device and on simulator
        //if NSDateUtility.isFormatTime24Hours() {
        //    dateFormatter.dateFormat = "h:mm a"
        //    return dateFormatter.stringFromDate(self)
        //}

        dateFormatter.dateFormat = "HH:mm"
        return dateFormatter.stringFromDate(self)
}

我在哪里做错了?

iOS版本到处都是9.1。

提前致谢任何答案!

Where am I doing wrong?
iOS version is 9.1 everywhere.
Thanks in advance for any answer!

[在Paul.s问题之后编辑]

在模拟器上它返回24h格式化日期(右)。

在设备上,它返回12h格式化日期(错误)。


On simulator it returns 24h formatted date (right).
On device it returns 12h formatted date (wrong).

推荐答案

QA1480 :将自己设置为 en_US_POSIX 的格式化程序的语言环境。否则,您设备的区域设置将影响日期模式。

QA1480: set yourself to be a a formatter's locale to en_US_POSIX. Otherwise your device's locale will affect date patterns.

这篇关于Swift将NSDate转换为24h格式在Device上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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