Swift 3.0 : Convert server UTC time to local time and vice-versa

后端 未结 7 1238
一生所求
一生所求 2020-12-02 08:09

I want to convert server UTC time to local time and vice-versa. Here is my code..

var isTimeFromServer = true
var time:String!
var period:String!
let timeStr         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 08:57

    For everyone using TimeZone objects. I would advise you to create your TimeZone from identifier rather than abbreviation when you have the possibility.

    This prevents errors caused by daylight saving.

    To illustrate my point let's take an example. You can instantiate like this let timeZone = TimeZone(identifier: "Europe/Paris") or like that let timeZone = TimeZone(abbreviation: "CEST") or "UTC +2:00"

    But this is time zone for summer CEST meaning Central Europe Summer Time We have CET meaning Central Europe Time for winter which is "UTC +1:00"

    You could manage daylight saving by your own with Date.isDaylightSavingsTime but this means more code and you don't have control on where your daylight saving sprang from. "indicates whether the receiver is currently using daylight saving time" from official doc

    All is that is to say favour TimeZone(identifier: ...)

提交回复
热议问题