获取 UTC 中的当前本地时间并设置为二进制运算符,如 >= 或 <
Get current local time in UTC and set to Binary operator like >= or <
请耐心等待,我是 iOS 开发的新手;我目前正在尝试根据设备的本地时间获取 UTC 的当前时间,并根据 UTC 的当前时间在 if / else 语句中设置它。
我试过以下方法:
let UTCdate = Date()
if (UTCdate >= 13 && UTCdate < 23 {
// do something
}
它给我的错误是“二元运算符'>='不能应用于'Date'和'Int'
类型的操作数
我知道我做错了什么,只是不知道是什么。如果已经有人问过这个问题,我们深表歉意。我google会尽我所能。
您需要从日期中提取小时数。 Date
是时间戳,不是一个小时。并且您需要确保在 UTC 时区而不是本地时间中提取小时。
以下将满足您的需求:
// Create a calendar with the UTC timezone
var utcCal = Calendar(identifier: Calendar.current.identifier) // or hardcode .gregorian if appropriate
utcCal.timeZone = TimeZone(secondsFromGMT: 0)!
// The current date
let date = Date()
// Get the hour (it will be UTC hour)
let hour = utcCal.component(.hour, from: date)
if hour >= 13 && hour < 23 {
}
如果您只是使用当前日历从 date
中增加小时,您将获得当地时间的小时。这就是我们创建特定于 UTC 时区的新日历的原因(因为这是您的要求)。
根据一些评论,您实际上可能不需要 UTC。只需将 timeZone
设置为您实际需要的任何时区。
如果您想要芝加哥时间,请使用:
utcCal.timeZone = TimeZone(identifier: "America/Chicago")!
创建您自己的日历实例的替代方法是使用:
let hour = Calendar.current.dateComponents(in: TimeZone(secondsFromGMT: 0)!, from: date).hour!
同样,使用您需要的任何时区。
你说的是"I'm currently trying to get the current time in UTC based on the device's local time"。您似乎没有理解 Cocoa 中关于 Date
个对象的关键问题。 Date
是地球上任何地方的瞬间。它没有时区。在内部,它表示为与以 UTC 表示的时刻的偏移量,称为 "epoch date",但这是一个实现细节。想象一下,当我使用调用 Date() 捕获 Date
时,我打响指,全世界同时听到打响指的声音,甚至没有光速延迟。手指弹响是对 Date()
的调用捕获的时刻。
如果我使用代码 let date = Date()
,我会捕获整个星球的当前时间。 Date
对象没有特定的时间,除非我将它转换为特定的时区。正如其他人所建议的那样,Calendar
方法 dateComponents(in:from:)
可以让您从 Date
中提取特定时区 中的小时 等组件,但是您需要了解所有这些是如何工作的,否则你会感到困惑。
我建议在 Xcode 帮助系统中搜索短语 "Calendrical Calculations" 并阅读文档的该部分。很有帮助。
请耐心等待,我是 iOS 开发的新手;我目前正在尝试根据设备的本地时间获取 UTC 的当前时间,并根据 UTC 的当前时间在 if / else 语句中设置它。
我试过以下方法:
let UTCdate = Date()
if (UTCdate >= 13 && UTCdate < 23 {
// do something
}
它给我的错误是“二元运算符'>='不能应用于'Date'和'Int'
类型的操作数我知道我做错了什么,只是不知道是什么。如果已经有人问过这个问题,我们深表歉意。我google会尽我所能。
您需要从日期中提取小时数。 Date
是时间戳,不是一个小时。并且您需要确保在 UTC 时区而不是本地时间中提取小时。
以下将满足您的需求:
// Create a calendar with the UTC timezone
var utcCal = Calendar(identifier: Calendar.current.identifier) // or hardcode .gregorian if appropriate
utcCal.timeZone = TimeZone(secondsFromGMT: 0)!
// The current date
let date = Date()
// Get the hour (it will be UTC hour)
let hour = utcCal.component(.hour, from: date)
if hour >= 13 && hour < 23 {
}
如果您只是使用当前日历从 date
中增加小时,您将获得当地时间的小时。这就是我们创建特定于 UTC 时区的新日历的原因(因为这是您的要求)。
根据一些评论,您实际上可能不需要 UTC。只需将 timeZone
设置为您实际需要的任何时区。
如果您想要芝加哥时间,请使用:
utcCal.timeZone = TimeZone(identifier: "America/Chicago")!
创建您自己的日历实例的替代方法是使用:
let hour = Calendar.current.dateComponents(in: TimeZone(secondsFromGMT: 0)!, from: date).hour!
同样,使用您需要的任何时区。
你说的是"I'm currently trying to get the current time in UTC based on the device's local time"。您似乎没有理解 Cocoa 中关于 Date
个对象的关键问题。 Date
是地球上任何地方的瞬间。它没有时区。在内部,它表示为与以 UTC 表示的时刻的偏移量,称为 "epoch date",但这是一个实现细节。想象一下,当我使用调用 Date() 捕获 Date
时,我打响指,全世界同时听到打响指的声音,甚至没有光速延迟。手指弹响是对 Date()
的调用捕获的时刻。
如果我使用代码 let date = Date()
,我会捕获整个星球的当前时间。 Date
对象没有特定的时间,除非我将它转换为特定的时区。正如其他人所建议的那样,Calendar
方法 dateComponents(in:from:)
可以让您从 Date
中提取特定时区 中的小时 等组件,但是您需要了解所有这些是如何工作的,否则你会感到困惑。
我建议在 Xcode 帮助系统中搜索短语 "Calendrical Calculations" 并阅读文档的该部分。很有帮助。