Fast NSDateFormatter not working

I am trying to format the time as follows:

let formatter = NSDateFormatter() formatter.dateFormat = "hh_mm_ss" let d = formatter.stringFromDate(NSDate()) println("formatted text is: \(d)") 

Suppose the current time is September 3, 2014 10:15:30 p.m.

  • When I run this script on the playground, it prints a correctly formatted time: 22_15_30 .
  • When you run this AppDelegate application, it does not print a formatted time: 22:15:30

I am using xcode 6 beta 5 ... Am I missing something? Why doesn't stringFromDate return the correct date in format?

EDIT : I am using xcode 6 beta 6.

Thanks!

+2
ios swift nsdate nsdateformatter
source share
1 answer

The 24-hour format is “HH”, not “hh”.

The reason that he works on the playground may be that user settings can override the choice of the 12/24 format, compare. What is the best way to deal with the NSDateFormatter language standard and "pay"? . To be safe, set the locale "en_US_POSIX" to format the date:

 formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") // Swift 3: formatter.locale = Locale(identifier: "en_US_POSIX") 
+7
source share

All Articles