[英]How to get the current date and time of another timezone in Swift?
I have a scheduled job that needs to run against different time windows in different timezones.我有一个计划的工作需要在不同时区的不同时间 windows 运行。 Ultimately, knowing UTC time when it's (for example) 20:00 in Australia, in order to kick off the job for this area.
最终,了解澳大利亚的(例如)20:00 时的 UTC 时间,以便开始该区域的工作。
UPDATE更新
More details about my use case: I have a scheduled job that needs to run every ie few hours.有关我的用例的更多详细信息:我有一个计划的工作需要每隔几个小时运行一次。 Which does some Redis cache cleaning for different clients.
哪个 Redis 为不同的客户端清理缓存。
This job needs to run when it is after office hours for each of my clients, which is in respect to their timezones.这项工作需要在我的每个客户的办公时间之后运行,这是关于他们的时区的。
I have the timezone of each of my clients.我有每个客户的时区。 However, I need to find out the time for each timezones in order to be able to run the clean up job for each of my clients.
但是,我需要找出每个时区的时间,以便能够为我的每个客户运行清理作业。
I was not able to find an example on how to get the time of another timezone in Swift?我找不到关于如何在 Swift 中获取另一个时区时间的示例?
There are a number of ways to get a local time, such as using string time zone identifiers and seconds from GMT.有多种方法可以获取本地时间,例如使用字符串时区标识符和 GMT 秒数。 And there are a number of formats to express time, such as strings and native
Date
objects.并且有多种格式可以表示时间,例如字符串和原生
Date
对象。 You didn't specify much in your question, so here's a starting point:您在问题中没有详细说明,所以这是一个起点:
func localTime(in timeZone: String) -> String {
let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime]
f.timeZone = TimeZone(identifier: timeZone)
return f.string(from: Date())
}
print(localTime(in: "Asia/Tehran")) // 2019-11-20T20:17:13+03:30 (8:17 PM)
How many time zones there are in the world is not entirely definitive but the general consensus appears to be 38. I prefer using secondsFromGMT
because it's more deterministic than using string identifiers because the string identifiers are subject to change (as this is a Swift library), while secondsFromGMT
cannot (without a change in the timezone itself).世界上有多少个时区并不完全确定,但普遍共识似乎是 38。我更喜欢使用
secondsFromGMT
,因为它比使用字符串标识符更具确定性,因为字符串标识符可能会发生变化(因为这是一个 Swift 库) ,而secondsFromGMT
不能(不改变时区本身)。
// For a list of all time zone string identifiers
for timeZone in TimeZone.knownTimeZoneIdentifiers {
print(timeZone)
}
Unfortunately, secondsFromGMT
does not recognize fractional time zones, and there are quite a few;不幸的是,
secondsFromGMT
不识别小数时区,而且有很多; therefore, we can use both methods to get a complete list of 38:因此,我们可以使用这两种方法来获得 38 个的完整列表:
-12:00 TimeZone(secondsFromGMT: -43200)
-12:00
TimeZone(secondsFromGMT: -43200)
-11:00 TimeZone(secondsFromGMT: -39600)
-11:00
TimeZone(secondsFromGMT: -39600)
-10:00 TimeZone(secondsFromGMT: -36000)
-10:00
TimeZone(secondsFromGMT: -36000)
-09:30 TimeZone(identifier: "Pacific/Marquesas")
-09:30
TimeZone(identifier: "Pacific/Marquesas")
-09:00 TimeZone(secondsFromGMT: -32400)
-09:00
TimeZone(secondsFromGMT: -32400)
-08:00 TimeZone(secondsFromGMT: -28800)
-08:00
TimeZone(secondsFromGMT: -28800)
-07:00 TimeZone(secondsFromGMT: -25200)
-07:00
TimeZone(secondsFromGMT: -25200)
-06:00 TimeZone(secondsFromGMT: -21600)
-06:00
TimeZone(secondsFromGMT: -21600)
-05:00 TimeZone(secondsFromGMT: -18000)
-05:00
TimeZone(secondsFromGMT: -18000)
-04:00 TimeZone(secondsFromGMT: -14400)
-04:00
TimeZone(secondsFromGMT: -14400)
-03:30 TimeZone(identifier: "America/St_Johns")
-03:30
TimeZone(identifier: "America/St_Johns")
-03:00 TimeZone(secondsFromGMT: -10800)
-03:00
TimeZone(secondsFromGMT: -10800)
-02:00 TimeZone(secondsFromGMT: -7200)
-02:00
TimeZone(secondsFromGMT: -7200)
-01:00 TimeZone(secondsFromGMT: -3600)
-01:00
TimeZone(secondsFromGMT: -3600)
+00:00 TimeZone(secondsFromGMT: 0)
+00:00
TimeZone(secondsFromGMT: 0)
+01:00 TimeZone(secondsFromGMT: 3600)
+01:00
TimeZone(secondsFromGMT: 3600)
+02:00 TimeZone(secondsFromGMT: 7200)
+02:00
TimeZone(secondsFromGMT: 7200)
+03:00 TimeZone(secondsFromGMT: 10800)
+03:00
TimeZone(secondsFromGMT: 10800)
+03:30 TimeZone(identifier: "Asia/Tehran")
+03:30
TimeZone(identifier: "Asia/Tehran")
+04:00 TimeZone(secondsFromGMT: 14400)
+04:00
TimeZone(secondsFromGMT: 14400)
+04:30 TimeZone(identifier: "Asia/Kabul")
+04:30
TimeZone(identifier: "Asia/Kabul")
+05:00 TimeZone(secondsFromGMT: 18000)
+05:00
TimeZone(secondsFromGMT: 18000)
+05:30 TimeZone(identifier: "Asia/Colombo")
+05:30
TimeZone(identifier: "Asia/Colombo")
+05:45 TimeZone(identifier: "Asia/Kathmandu")
+05:45
TimeZone(identifier: "Asia/Kathmandu")
+06:00 TimeZone(secondsFromGMT: 21600)
+06:00
TimeZone(secondsFromGMT: 21600)
+06:30 TimeZone(identifier: "Asia/Yangon")
+06:30
TimeZone(identifier: "Asia/Yangon")
+07:00 TimeZone(secondsFromGMT: 25200)
+07:00
TimeZone(secondsFromGMT: 25200)
+08:00 TimeZone(secondsFromGMT: 28800)
+08:00
TimeZone(secondsFromGMT: 28800)
+08:45 TimeZone(identifier: "Australia/Eucla")
+08:45
TimeZone(identifier: "Australia/Eucla")
+09:00 TimeZone(secondsFromGMT: 32400)
+09:00
TimeZone(secondsFromGMT: 32400)
+09:30 TimeZone(identifier: "Australia/Adelaide")
+09:30
TimeZone(identifier: "Australia/Adelaide")
+10:00 TimeZone(secondsFromGMT: 36000)
+10:00
TimeZone(secondsFromGMT: 36000)
+10:30 TimeZone(identifier: "Australia/Lord_Howe")
+10:30
TimeZone(identifier: "Australia/Lord_Howe")
+11:00 TimeZone(secondsFromGMT: 39600)
+11:00
TimeZone(secondsFromGMT: 39600)
+12:00 TimeZone(secondsFromGMT: 43200)
+12:00
TimeZone(secondsFromGMT: 43200)
+12:45 TimeZone(identifier: "Pacific/Chatham")
+12:45
TimeZone(identifier: "Pacific/Chatham")
+13:00 TimeZone(secondsFromGMT: 46800)
+13:00
TimeZone(secondsFromGMT: 46800)
+14:00 TimeZone(secondsFromGMT: 50400)
+14:00
TimeZone(secondsFromGMT: 50400)
func getDateAndTime(timeZoneIdentifier: String) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
dateFormatter.timeZone = TimeZone(identifier: timeZoneIdentifier)
return dateFormatter.string(from: Date())
}
print(getDateAndTime(timeZoneIdentifier: "UTC")) //2019-11-20 23:37:28 091
You need to convert UTC time into local timezone.您需要将 UTC 时间转换为本地时区。 Here is the code to convert it.
这是转换它的代码。
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss SSS"
formatter.locale = Locale.current
formatter.timeZone = TimeZone.current
let date = dateFormatter.date(from: utcDate)
Here change utcDate with date you want to convert to local.在此处将 utcDate 更改为要转换为本地的日期。
I wanted the time in a simple format.我想要一个简单格式的时间。 ie
11:23pm
in the users current locale setting.即用户当前区域设置中的
11:23pm
。
Here's how I did it:我是这样做的:
func getTimeWithLocale(with timeZone: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.timeZone = TimeZone(identifier: timeZone)
dateFormatter.locale = .current
let time = dateFormatter.string(from: Date()).lowercased().replacingOccurrences(of: " ", with: "")
return time
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.