问题
In my App if IPhone device time is in 12 hour formate then date formatter works correctly but if device time is in 24 hour formate then app crashed.
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)
回答1:
@Rajan Thanks for giving me idea of NSLocale
. I set the dateformatter's locale identifier to "en_US_POSIX
".
i just add this below line in my code after allocating date formatter. dateformatter.locale = NSLocale(localeIdentifier:"en_US_POSIX")
Thanks frnd for giving this idea
回答2:
Nandini answer is correct. But the only issue is that if you harcode the locale, the dates are displayed only in english.
In my case I wanted it in french, here is a workaround that worked for me in Swift 3 :
dateformatter.locale = Locale(identifier: NSLocale.current.identifier)
回答3:
Assuming that you are using Swift 2.x version after seeing your code.
The problem is because your dateFormatter
contains a
and for the 24 Hour Format time we don't have a
So in order to fix your problem, you have to check whether your device is in 24-Hour format or 12 Hour format
Taking your array's index object in a variable time
and keep that as var
because we will change that later.
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!)
And we need a function to make your time string converted to 24 hour format if your phone is in 24 Hour Format.
Function defination of convertTo24HourFormat
is
//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
}
But converting only the time to date object don't make any sense as it will give a wrong date.
NOTE:-
The convertTo24HourFormat
function will only work fine if you send the time in the format hh:mm a
. For example 07:45 AM
回答4:
I had to change my formatter from this:
dateFormatter.dateFormat = "dd MM yyyy hh:mm:ss.SSS"
to this:
dateFormatter.dateFormat = "dd MM yyyy HH:mm:ss.SSS"
来源:https://stackoverflow.com/questions/40055740/app-crashed-when-device-time-is-in-24-hour-format