swift NSDateFormatter not working

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 03:20:42

问题


I'm trying to format time as follows:

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

Suppose that current time is Sep 3, 2014 22:15:30

  1. When I run this script on playground, it prints the time correctly formatted: 22_15_30.
  2. When running this on AppDelegate, it doesn't print the time formatted: 22:15:30

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

EDIT: I'm using xcode 6 beta 6.

Thanks!


回答1:


The 24-hour format is "HH", not "hh".

The reason that it works in the Playground may be that user defined settings can override the 12/24-format choice, compare What is the best way to deal with the NSDateFormatter locale "feechur"?. To be on the safe side, set the "en_US_POSIX" locale for the date formatter:

formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

// Swift 3:
formatter.locale = Locale(identifier: "en_US_POSIX")


来源:https://stackoverflow.com/questions/25657123/swift-nsdateformatter-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!