Get UTC time and local time from NSDate object

前端 未结 10 1019
夕颜
夕颜 2020-11-28 23:17

In objective-c, the following code results in the UTC date time information using the date API.

NSDate *currentUTCDate = [NSDate date]
         


        
10条回答
  •  悲哀的现实
    2020-11-28 23:46

    Swift 3

    You can get Date based on your current timezone from UTC

    extension Date {
        func currentTimeZoneDate() -> String {
            let dtf = DateFormatter()
            dtf.timeZone = TimeZone.current
            dtf.dateFormat = "yyyy-MM-dd HH:mm:ss"
    
            return dtf.string(from: self)
        }
    }
    

    Call like this:

    Date().currentTimeZoneDate()
    

提交回复
热议问题