不同时区的相同倒计时-Swift

不同时区的相同倒计时-Swift,swift,calendar,timezone,countdown,Swift,Calendar,Timezone,Countdown,我已经创建了一个倒数计时,它在CEST时区运行良好,但我希望它能显示所有时区不同的国家剩余的相同正确时间 你知道我如何操作代码吗 // here we set the current date let date = NSDate() let calendar = Calendar.current let components = calendar.dateComponents([.hour, .minute, .month, .year, .day], from: d

我已经创建了一个倒数计时,它在CEST时区运行良好,但我希望它能显示所有时区不同的国家剩余的相同正确时间

你知道我如何操作代码吗

 // here we set the current date
    let date = NSDate()
    let calendar = Calendar.current

    let components = calendar.dateComponents([.hour, .minute, .month, .year, .day], from: date as Date)

    let currentDate = calendar.date(from: components)

    let userCalendar = Calendar.current

    // here we set the due date. When the timer is supposed to finish
    let competitionDate = NSDateComponents()
    competitionDate.year = year
    competitionDate.month = month
    competitionDate.day = day
    competitionDate.hour = hour
    competitionDate.minute = minute
    let competitionDay = userCalendar.date(from: competitionDate as DateComponents)!

    //here we change the seconds to hours,minutes and days
    let competitionDayDifference = calendar.dateComponents([.day, .hour, .minute], from: currentDate!, to: competitionDay)


    //finally, here we set the variable to our remaining time
    let daysLeft = competitionDayDifference.day
    let hoursLeft = competitionDayDifference.hour
    let minutesLeft = competitionDayDifference.minute

    print("day:", daysLeft ?? "N/A", "hour:", hoursLeft ?? "N/A", "minute:", minutesLeft ?? "N/A")

您当前的代码不起作用,因为比赛日不是以时间点(自1970年以来的x秒)表示的,而是以本地日期时间(年、月、日、小时、分钟等)表示的

要将比赛日期表示为一个时间点,您需要将其与您尚未提供的时区相关联。您可以向
日历
提供一个日历,用于从日期组件获取
日期

var userCalendar = Calendar.current
userCalendar.timeZone = TimeZone(identifier: "...")!
然后,无论设备处于哪个时区,比赛日期都将转换为相同的时间点

或者,设置
competitionDate.timeZone

competitionDate.timeZone = TimeZone(identifier: "...")!

您当前的代码不起作用,因为比赛日不是以时间点(自1970年以来的x秒)表示的,而是以本地日期时间(年、月、日、小时、分钟等)表示的

要将比赛日期表示为一个时间点,您需要将其与您尚未提供的时区相关联。您可以向
日历
提供一个日历,用于从日期组件获取
日期

var userCalendar = Calendar.current
userCalendar.timeZone = TimeZone(identifier: "...")!
然后,无论设备处于哪个时区,比赛日期都将转换为相同的时间点

或者,设置
competitionDate.timeZone

competitionDate.timeZone = TimeZone(identifier: "...")!

变量
year
、month、day`等从何而来?此代码封装在一个函数中,因此年、月、日等在?CEST时区中的哪个时区是
year
month
day
hour
minute
,因此,基本上我希望它能在所有其他时区保持相同的剩余时间。你能告诉我具体修改什么吗?请询问变量
、月、日等从何而来?此代码包装在一个函数中,因此年、月、日等在?CEST时区中的
小时
分钟
,因此,基本上我希望它能在所有其他时区保持相同的剩余时间。你能告诉我具体修改什么吗?那太棒了!我没想过这个。。。非常感谢:)更好地使用UTC/GMT。比较(重点补充):一般来说,除了“GMT”这样的独特实例外,不鼓励您使用缩写。时区缩写没有标准化,因此给定的缩写可能有多种含义,例如,“EST”指美国和澳大利亚的东部时间。@MartinR OP不应使用UTC/GMT,因为给定的年/月/日/小时/分钟不是UTC/GMT。OP应使用给定日期所在的任何时区,所以我想我会让OP选择一个时区标识符。@Sweeper:我只是想说
时区(缩写:“CEST”)
不是一个好的选择(为什么)。如果最终日期是从服务器检索的,那么我可能会让服务器生成一个Unix时间戳。这太棒了!我没想过这个。。。非常感谢:)更好地使用UTC/GMT。比较(重点补充):一般来说,除了“GMT”这样的独特实例外,不鼓励您使用缩写。时区缩写没有标准化,因此给定的缩写可能有多种含义,例如,“EST”指美国和澳大利亚的东部时间。@MartinR OP不应使用UTC/GMT,因为给定的年/月/日/小时/分钟不是UTC/GMT。OP应使用给定日期所在的任何时区,所以我想我会让OP选择一个时区标识符。@Sweeper:我只是想说
时区(缩写:“CEST”)
不是一个好的选择(为什么)。如果从服务器检索到最终日期,那么我可能会让服务器生成一个Unix时间戳。