NSDate set timezone in swift

前端 未结 5 1897
一向
一向 2020-12-10 04:25

how can i return a NSDate in a predefined time zone from a string

let responseString = \"2015-8-17 GMT+05:30\"
var dFormatter = NSDateFormatter()
dFormatter.         


        
5条回答
  •  鱼传尺愫
    2020-12-10 04:41

    how can i return a NSDate in a predefined time zone?

    You can't.

    An instance of NSDate does not carry any information about timezone or calendar. It just simply identifies one point in universal time.

    You can interpret this NSDate object in whatever calendar you want. Swift's string interpolation (the last line of your example code) uses an NSDateFormatter that uses UTC (that's the "+0000" in the output).

    If you want the NSDate's value as a string in the current user's calendar you have to explicitly set up a date formatter for that.

提交回复
热议问题