Omi*_*mid 3 nsdate foundation swift swift2
在我的应用程序中(使用Swift 2)我有一个使用的功能NSDate,但是我有一个问题:根据设备上设置的语言,获得的日期不一样(例如,月份取决于语言:1月份的英语是等于法语中的Janvier)
当我有这个:
NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.NoStyle)
Run Code Online (Sandbox Code Playgroud)
当我在美国本地化时,我得到了这种格式,3/29/16
但是当我在法国本地化时,我得到了不同的格式:29/03/16
无论语言是什么,我怎么能要求相同的日期格式(这里是美国格式)?
用这个,
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .ShortStyle
dateFormatter.timeStyle = .NoStyle
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
Run Code Online (Sandbox Code Playgroud)
具有相同格式的关键始终使用相同的区域设置.因此,它使用英语语言环境来格式化字符串,因此它始终保持格式
M/d/yy
Run Code Online (Sandbox Code Playgroud)