繁体   English   中英

如何获取Swift中另一个时区的当前日期和时间?

[英]How to get the current date and time of another timezone in Swift?

我有一个计划的工作需要在不同时区的不同时间 windows 运行。 最终,了解澳大利亚的(例如)20:00 时的 UTC 时间,以便开始该区域的工作。

更新

有关我的用例的更多详细信息:我有一个计划的工作需要每隔几个小时运行一次。 哪个 Redis 为不同的客户端清理缓存。

这项工作需要在我的每个客户的办公时间之后运行,这是关于他们的时区的。

我有每个客户的时区。 但是,我需要找出每个时区的时间,以便能够为我的每个客户运行清理作业。

我找不到关于如何在 Swift 中获取另一个时区时间的示例?

有多种方法可以获取本地时间,例如使用字符串时区标识符和 GMT 秒数。 并且有多种格式可以表示时间,例如字符串和原生Date对象。 您在问题中没有详细说明,所以这是一个起点:

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)

世界上有多少个时区并不完全确定,但普遍共识似乎是 38。我更喜欢使用secondsFromGMT ,因为它比使用字符串标识符更具确定性,因为字符串标识符可能会发生变化(因为这是一个 Swift 库) ,而secondsFromGMT不能(不改变时区本身)。

// For a list of all time zone string identifiers
for timeZone in TimeZone.knownTimeZoneIdentifiers {
    print(timeZone)
}

不幸的是, secondsFromGMT不识别小数时区,而且有很多; 因此,我们可以使用这两种方法来获得 38 个的完整列表:

-12:00 TimeZone(secondsFromGMT: -43200)
-11:00 TimeZone(secondsFromGMT: -39600)
-10:00 TimeZone(secondsFromGMT: -36000)
-09:30 TimeZone(identifier: "Pacific/Marquesas")
-09:00 TimeZone(secondsFromGMT: -32400)
-08:00 TimeZone(secondsFromGMT: -28800)
-07:00 TimeZone(secondsFromGMT: -25200)
-06:00 TimeZone(secondsFromGMT: -21600)
-05:00 TimeZone(secondsFromGMT: -18000)
-04:00 TimeZone(secondsFromGMT: -14400)
-03:30 TimeZone(identifier: "America/St_Johns")
-03:00 TimeZone(secondsFromGMT: -10800)
-02:00 TimeZone(secondsFromGMT: -7200)
-01:00 TimeZone(secondsFromGMT: -3600)
+00:00 TimeZone(secondsFromGMT: 0)
+01:00 TimeZone(secondsFromGMT: 3600)
+02:00 TimeZone(secondsFromGMT: 7200)
+03:00 TimeZone(secondsFromGMT: 10800)
+03:30 TimeZone(identifier: "Asia/Tehran")
+04:00 TimeZone(secondsFromGMT: 14400)
+04:30 TimeZone(identifier: "Asia/Kabul")
+05:00 TimeZone(secondsFromGMT: 18000)
+05:30 TimeZone(identifier: "Asia/Colombo")
+05:45 TimeZone(identifier: "Asia/Kathmandu")
+06:00 TimeZone(secondsFromGMT: 21600)
+06:30 TimeZone(identifier: "Asia/Yangon")
+07:00 TimeZone(secondsFromGMT: 25200)
+08:00 TimeZone(secondsFromGMT: 28800)
+08:45 TimeZone(identifier: "Australia/Eucla")
+09:00 TimeZone(secondsFromGMT: 32400)
+09:30 TimeZone(identifier: "Australia/Adelaide")
+10:00 TimeZone(secondsFromGMT: 36000)
+10:30 TimeZone(identifier: "Australia/Lord_Howe")
+11:00 TimeZone(secondsFromGMT: 39600)
+12:00 TimeZone(secondsFromGMT: 43200)
+12:45 TimeZone(identifier: "Pacific/Chatham")
+13:00 TimeZone(secondsFromGMT: 46800)
+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

您需要将 UTC 时间转换为本地时区。 这是转换它的代码。

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)

在此处将 utcDate 更改为要转换为本地的日期。

我想要一个简单格式的时间。 即用户当前区域设置中的11:23pm

我是这样做的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM