简体   繁体   English

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

[英]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.

相关问题 如何从 Swift 中的时间服务器获取当前时区的当前日期? - How to get current date from current timezone from a time server in Swift? 如何获取特定时区的当前时间? - How to get the current time in a specific timezone? 如何通过基金会迅速获得当前的日期和时间 - How can I get current date and time in swift with out Foundation 从UTC-5时区日期和时间iPhone应用程序获取当前iPhone设备的时区日期和时间? - Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app? 如何从给定的日期时间响应中获取当地时区的商店营业时间? - SWIFT 5 - How to get store opening hours in local timezone from given date-time response? - SWIFT 5 如何获得Swift的当前时间? - How to get current time in Swift? Swift:如何使用时区缩写解析日期和时间字符串? - Swift: How to parse date & time string with timezone abbreviation? 如何获取当前 EST 时间 IOS Swift - How to get Current EST time IOS Swift 这个 Swift TimeZone 是另一个时区假设中日期的缩写有什么问题? - What is wrong with this Swift TimeZone for abbreviation for a date in another timezone assumption? iOS Swift - 获取当前本地时间和日期时间戳 - iOS Swift - Get the Current Local Time and Date Timestamp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM