当设备时间为 24 小时格式时应用程序崩溃
App Crashed when device time is in 24 hour format
在我的应用程序中,如果 IPhone 设备时间为 12 小时格式,日期格式化程序可以正常工作,但如果设备时间为 24 小时格式,则应用程序崩溃。
let dateFormatter = NSDateFormatter();
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle;
dateFormatter.dateFormat = "hh:mm a";
var dt1 = dateFormatter.dateFromString(arrSecond.objectAtIndex(n) as! String)
假设您在看到您的代码后使用的是 Swift 2.x 版本。
问题是因为您的 dateFormatter
包含 a
而我们没有 a
的 24 小时制时间
所以为了解决您的问题,您必须检查您的设备是 24 小时制还是 12 小时制
将数组的索引对象放入变量 time
并将其保留为 var
因为我们稍后会更改它。
var time = arrSecond.objectAtIndex(n) as! String //this contains 07:45 AM
//Now create your Date Formatter
let dateFormatter = NSDateFormatter();
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle;
//Now check whether your phone is in 12 hour or 24 hour format
let locale = NSLocale.currentLocale()
let formatter : String = NSDateFormatter.dateFormatFromTemplate("j", options:0, locale:locale)!
//we have to change our dateFormatter as per the hour format
if formatter.containsString("a") {
print("phone is in 12 Hour Format")
dateFormatter.dateFormat = "hh:mm a";
} else {
print("phone is in 24 Hour Format")
time = convertTo24HourFormat(time) //time changed to 24 Hour format and AM/PM removed from string
dateFormatter.dateFormat = "HH:mm";
}
let finalDate = dateFormatter.dateFromString(time)
print(finalDate!)
如果您的 phone 是 24 小时格式,我们需要一个函数将您的时间字符串转换为 24 小时格式。
convertTo24HourFormat
的函数定义是
//Example 08:45 PM to 20:45
func convertTo24HourFormat(time:String) -> String {
var formattedTime = time
if formattedTime.containsString("PM") {
var hour = Int(formattedTime.substringToIndex(formattedTime.startIndex.advancedBy(2)))
if hour != 12 {
hour = hour! + 12
formattedTime.removeRange(Range<String.Index>(start: formattedTime.startIndex, end: formattedTime.startIndex.advancedBy(2)))
formattedTime = "\(hour!)\(formattedTime)"
}
} else {
// For AM time
var hour = Int(formattedTime.substringToIndex(formattedTime.startIndex.advancedBy(2)))
//case for 12 AM
if hour == 12 {
formattedTime.removeRange(Range<String.Index>(start: formattedTime.startIndex, end: formattedTime.startIndex.advancedBy(2)))
formattedTime = "00\(formattedTime)"
}
}
formattedTime = formattedTime.substringToIndex(time.startIndex.advancedBy(5))
return formattedTime
}
但是只转换时间到日期对象没有任何意义,因为它会给出错误的日期。
注意:-
convertTo24HourFormat
函数只有在您以 hh:mm a
格式发送时间时才能正常工作。例如 07:45 AM
@Rajan 感谢您让我想到 NSLocale
。我将 dateformatter 的区域设置标识符设置为“en_US_POSIX
”。
在分配日期格式化程序后,我只是在我的代码中添加下面的行。 dateformatter.locale = NSLocale(localeIdentifier:"en_US_POSIX")
感谢 frnd 提出这个想法
我不得不改变我的格式化程序:
dateFormatter.dateFormat = "dd MM yyyy hh:mm:ss.SSS"
对此:
dateFormatter.dateFormat = "dd MM yyyy HH:mm:ss.SSS"
南迪尼回答正确。但唯一的问题是,如果您对语言环境进行硬编码,则日期仅以英文显示。
在我的例子中,我想要它的法语版本,这里有一个在 Swift 3 中对我有用的解决方法:
dateformatter.locale = Locale(identifier: NSLocale.current.identifier)
在我的应用程序中,如果 IPhone 设备时间为 12 小时格式,日期格式化程序可以正常工作,但如果设备时间为 24 小时格式,则应用程序崩溃。
let dateFormatter = NSDateFormatter();
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle;
dateFormatter.dateFormat = "hh:mm a";
var dt1 = dateFormatter.dateFromString(arrSecond.objectAtIndex(n) as! String)
假设您在看到您的代码后使用的是 Swift 2.x 版本。
问题是因为您的 dateFormatter
包含 a
而我们没有 a
所以为了解决您的问题,您必须检查您的设备是 24 小时制还是 12 小时制
将数组的索引对象放入变量 time
并将其保留为 var
因为我们稍后会更改它。
var time = arrSecond.objectAtIndex(n) as! String //this contains 07:45 AM
//Now create your Date Formatter
let dateFormatter = NSDateFormatter();
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle;
//Now check whether your phone is in 12 hour or 24 hour format
let locale = NSLocale.currentLocale()
let formatter : String = NSDateFormatter.dateFormatFromTemplate("j", options:0, locale:locale)!
//we have to change our dateFormatter as per the hour format
if formatter.containsString("a") {
print("phone is in 12 Hour Format")
dateFormatter.dateFormat = "hh:mm a";
} else {
print("phone is in 24 Hour Format")
time = convertTo24HourFormat(time) //time changed to 24 Hour format and AM/PM removed from string
dateFormatter.dateFormat = "HH:mm";
}
let finalDate = dateFormatter.dateFromString(time)
print(finalDate!)
如果您的 phone 是 24 小时格式,我们需要一个函数将您的时间字符串转换为 24 小时格式。convertTo24HourFormat
的函数定义是
//Example 08:45 PM to 20:45
func convertTo24HourFormat(time:String) -> String {
var formattedTime = time
if formattedTime.containsString("PM") {
var hour = Int(formattedTime.substringToIndex(formattedTime.startIndex.advancedBy(2)))
if hour != 12 {
hour = hour! + 12
formattedTime.removeRange(Range<String.Index>(start: formattedTime.startIndex, end: formattedTime.startIndex.advancedBy(2)))
formattedTime = "\(hour!)\(formattedTime)"
}
} else {
// For AM time
var hour = Int(formattedTime.substringToIndex(formattedTime.startIndex.advancedBy(2)))
//case for 12 AM
if hour == 12 {
formattedTime.removeRange(Range<String.Index>(start: formattedTime.startIndex, end: formattedTime.startIndex.advancedBy(2)))
formattedTime = "00\(formattedTime)"
}
}
formattedTime = formattedTime.substringToIndex(time.startIndex.advancedBy(5))
return formattedTime
}
但是只转换时间到日期对象没有任何意义,因为它会给出错误的日期。
注意:-
convertTo24HourFormat
函数只有在您以 hh:mm a
格式发送时间时才能正常工作。例如 07:45 AM
@Rajan 感谢您让我想到 NSLocale
。我将 dateformatter 的区域设置标识符设置为“en_US_POSIX
”。
在分配日期格式化程序后,我只是在我的代码中添加下面的行。 dateformatter.locale = NSLocale(localeIdentifier:"en_US_POSIX")
感谢 frnd 提出这个想法
我不得不改变我的格式化程序:
dateFormatter.dateFormat = "dd MM yyyy hh:mm:ss.SSS"
对此:
dateFormatter.dateFormat = "dd MM yyyy HH:mm:ss.SSS"
南迪尼回答正确。但唯一的问题是,如果您对语言环境进行硬编码,则日期仅以英文显示。
在我的例子中,我想要它的法语版本,这里有一个在 Swift 3 中对我有用的解决方法:
dateformatter.locale = Locale(identifier: NSLocale.current.identifier)