Search code examples
swiftnsdatensdateformatter

App Crashed when device time is in 24 hour format


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)

Solution

  • @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