Home  >  Article  >  Web Front-end  >  swift_将UIDatePicker获取到的时间传到下一个页面出现相差几个小时的问题_html/css_WEB-ITnose

swift_将UIDatePicker获取到的时间传到下一个页面出现相差几个小时的问题_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:57:401240browse

今天,在写swift demo的时候遇到了一个很奇葩的问题,我再 present 出来的页面中利用 UIDatePicker 选取时间,然后再将获取到的时间传回原来的主界面,结果问题出现了:B页面获取时间正常,传回A页面后时间少了9个小时。百思不得其解,Google找了一些答案说是要这样:

 time.timeZone = NSTimeZone.systemTimeZone() time.calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
加了之后也没有什么作用,还是一样相差9个小时。后来有找到另外一种解决办法这样:

let timeZone = NSTimeZone.systemTimeZone()let seconds = timeZone.secondsFromGMTForDate(addObject.time.date)let correctDate = addObject.time.date.dateByAddingTimeInterval(Double(seconds))

虽然说问题解决了,但是还是有点不明白为什么会出现这样的情况,难道是 XCode GM版的原因(不太可能啊~)。希望知道啥原因的童鞋不吝赐教,共同进步!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn