在我的应用程序(使用 Swift 2)中,我有一个使用 NSDate
的函数,但是我遇到了一个问题:根据设备上设置的语言,获得的日期不一样(对于例如月份取决于语言:英语中的 January 等于法语中的 Janvier)
当我有这个时:
NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.ShortStyle, timeStyle: NSDateFormatterStyle.NoStyle)
当我在美国本地化时,我得到这种格式 3/29/16
但是当我在法国本地化时,我会得到一种不同的格式:29/03/16
无论语言是什么,我如何要求相同的日期格式(这里是美国格式)?
最佳答案
使用这个,
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .ShortStyle
dateFormatter.timeStyle = .NoStyle
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
具有相同格式的关键是始终使用相同的语言环境。因此,它使用英语语言环境来格式化字符串,因此它始终保持格式
M/d/yy
关于swift - NSDate 格式根据语言(地区)变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36283343/