gpt4 book ai didi

ios - swift 4 : Set Different Date and Time

转载 作者:搜寻专家 更新时间:2023-10-30 22:26:44 24 4
gpt4 key购买 nike

我知道如何获取本地日期和时间,但我想做的是从不同地方获取日期和时间。例如,我想知道纽约的时间和日期。我该如何解决这个简单的问题?

这是我的本地日期和时间代码:

let date = NSDate()
let calendar = Calendar.current

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

let currentDate = calendar.date(from: components)

我在这里搜索过,但没有找到我想要的,我仍在寻找日期库。如果您知道可以重定向我的任何来源或样本,我将不胜感激。

最佳答案

这里涉及几个不同的概念,我们需要(几乎)理解所有这些概念才能正确...

1) Date (NSDate,在 Swift 中)是一个绝对时间点 - 它的命名有点错误,因为它与 2017 年 11 月 13 日这样的实际日期无关,因为要达到这一点我们需要定义......

2) Calendar ,因为西方公历 2017 年 11 月 13 日也可能是伊斯兰历的 23 日 Safar 1439,或希伯来历的 Heshvan 5778 的 24 日,或 iOS 和 MacOS 支持的许多其他日历中的其他一些东西;

3)依次Calendar不仅更改了 DateComponents 中返回的值我们必须使用它来解压 Date + Calendar分为日、月、年和纪元(例如公元前/公元),甚至周数等...,而且一些日历可能与其他日历的组成部分不同

4) 一天中的时间(如您所知)取决于 TimeZone ,因此相同的绝对时间可以是许多不同的“点钟”时间之一,具体取决于您所在的位置。它还可能(如您在下面的示例中所见)更改日期 以及“时间”。这当然可以是自动的(你在哪里)或由程序员设置;

5) 此外,我们有DateFormatter (这是包裹 DateComponents 的便利),因为 2017 年 11 月 13 日可以表示为 13/11/17 或 11/13/17,具体取决于您是英国人还是美国人。我们可能还希望选择是使用文本还是数字月份,如果显示时间,我们是否需要 12 小时或 24 小时格式 - 所有这些都在 DateFormatter 中涵盖。 , but 如果您是法国人,文本表示可能是“13e Novembre 2017”,这引入了

的概念

6) Locale , 可以设置,如 TimeZone ,作为默认值(在您设置设备时选择)或由程序员指定。

您发布的代码将不起作用,因为它所做的只是需要一个 Date。 , 通过 Calendar 转换它至 DateComponents (到目前为止一切都很好),但随后重新创建了一个 Date从组件中 - 您将得到原始的 Date - 相同的绝对时间点。

我从这个问题和你在评论中对问题的回答中相信,你想要一个需要绝对时间(例如“现在”)的函数,也就是 Date并将其显示在特定的 TimeZone 中.这有效:

func timeComponents(date: Date, timeZone: TimeZone) -> DateComponents {
var calendar = Calendar.current
calendar.timeZone = timeZone
return calendar.dateComponents([.hour, .minute, .month, .year, .day, .second, .weekOfMonth], from: date)
}

let absTime: Date = Date() // Now
let edinburgh = TimeZone(abbreviation: "GMT")!
let newYork = TimeZone(abbreviation: "EST")!
let ec = timeComponents(date: absTime, timeZone: edinburgh)
let nycc = timeComponents(date: absTime, timeZone: newYork)
print(ec)// year: 2017 month: 11 day: 14 hour: 0 minute: 44 second: 10 weekOfMonth: 3 isLeapMonth: false
print(nycc) // year: 2017 month: 11 day: 13 hour: 19 minute: 44 second: 10 weekOfMonth: 3 isLeapMonth: false

...我认为这至少回答了你的问题,但为了巧妙地解决它,我们需要从 DateComponents 开始至 DateFormatter

func timeString(date: Date, timeZone: TimeZone, timeStyle: DateFormatter.Style) -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = timeZone
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = timeStyle
return dateFormatter.string(from: date)
}

let es = timeString(date: absTime, timeZone: edinburgh, timeStyle: .full)
let nycs = timeString(date: absTime, timeZone: newYork, timeStyle: .full)
print(es) // 12:44:10 AM Greenwich Mean Time
print(nycs) // 7:44:10 PM Eastern Standard Time

你可以继续,开始使用Locale ,如果你想国际化你的应用程序,但我把它留作练习!

附注这些不是所有概念 - 参见 here

p.p.s.另见 this answerthis answer (均不重复)

关于ios - swift 4 : Set Different Date and Time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47271340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com