Why NSCalendar's dateFromComponents returns wrong date?

后端 未结 2 1614
独厮守ぢ
独厮守ぢ 2020-12-11 19:45

I am expecting 2010-10-11 00:00:00

But instead I receive 2010-10-10 21:00:00 +0000

I know, that I can add a few hours and receive desired date, but I do not

相关标签:
2条回答
  • 2020-12-11 20:09

    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

    0 讨论(0)
  • 2020-12-11 20:23

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

    0 讨论(0)
提交回复
热议问题