获取特定时区的当前日期

时间:2019-08-05 20:22:57

标签: swift timezone nsdate nscalendar

我正在尝试获取用户选择的时区中的当前日期,但是尽管使用了stackoverflow上其他答案中的函数,但我无法获得除当前日期以外的任何内容。我用的是这个

func dateIn(timeZone: TimeZone) -> Date {
let now = Date()
var calendar = Calendar.current
calendar.timeZone = timeZone
let components = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)

let date = calendar.date(from: components) ?? Date()


   return date
}

如果有人可以帮助我理解为什么我将日历的时区更改为该时区,为什么我没有得到该时区的日期,我将非常感激。

0 个答案:

没有答案
相关问题