iOS-NSDate包含错误的小时值
我正在运行iOS 10.0.2,并将其升级到iOS 10.1.1,但存在相同的问题,每当尝试使用iOS-NSDate包含错误的小时值,ios,iphone,swift,nsdate,nsdateformatter,Ios,Iphone,Swift,Nsdate,Nsdateformatter,我正在运行iOS 10.0.2,并将其升级到iOS 10.1.1,但存在相同的问题,每当尝试使用NSDate时,它的小时值都不正确,例如: let now = NSDate() let dateFormatter = NSDateFormatter() dateFormatter.timeZone = NSTimeZone.localTimeZone() dateFormatter.dateFormat = "HH:mm" timeLabel.text = dateFormatter.strin
NSDate
时,它的小时值都不正确,例如:
let now = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = "HH:mm"
timeLabel.text = dateFormatter.stringFromDate(now)
它将在timeLabel
中显示为1212:15
,当前时间是12:15
,是iOS错误还是我的设备有问题,它是iPhone 6s
我已经将现在
变量打印到控制台,它有这个值2016-11-2399:31:16am+0000
,应该是这样的2016-11-2309:31:16am+0000
因此,NSDate()
的模式如下:
2016-11-23 1414:31:16 AM +0000
2016-11-23 2222:31:16 AM +0000
2016-11-23 2020:31:16 AM +0000
2016-11-23 1212:31:16 AM +0000
我在操场上试过这个。它工作得很好。我认为你的电脑没有问题code@KrishnaCA,是的,在其他设备上,代码工作正常,只是在提到的设备上它不工作,并给出这些奇怪的值。您是否尝试重新启动设备?设置中可能正在更改时区可能是您的
NSTimeZone.local()
问题。通常,如果您希望在24小时内更改时区,可以使用dateFormatter.locale=NSLocale(localeIdentifier:“en_US_POSIX”)
。那么你的dateFormatter.dateFormat=“HH:mm”
应该可以正常工作。我在操场上试过这个。它工作得很好。我认为你的电脑没有问题code@KrishnaCA,是的,在其他设备上,代码工作正常,只是在提到的设备上它不工作,并给出这些奇怪的值。您是否尝试重新启动设备?设置中可能正在更改时区可能是您的NSTimeZone.local()
问题。通常,如果您希望在24小时内更改时区,可以使用dateFormatter.locale=NSLocale(localeIdentifier:“en_US_POSIX”)
。那么您的dateFormatter.dateFormat=“HH:mm”
应该可以正常工作。