Why NSCalendar's dateFromComponents returns wrong date?

别说谁变了你拦得住时间么 提交于 2019-11-28 13:46:13
KingofBliss

I think your problem may be solved by setting the NSTimeZone.

If you need a specific time format use that formatter or else write this to get correct time. first option...

let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    let localDate = formatter.dateFromString("1989-10-17T05:00:00.000-0000")

or if you need date from components use the below code like ...

let calendar = NSCalendar.currentCalendar()
    calendar.timeZone = NSTimeZone(name: "GMT")!
    let date = calendar.dateFromComponents(components)

or you need

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