gpt4 book ai didi

ios - Swift - 获取本地日期和时间

转载 作者:IT王子 更新时间:2023-10-29 05:11:40 24 4
gpt4 key购买 nike

如何在 Swift 中获取本地日期和时间?

let last_login = String(NSDate.date())

最佳答案

更新:Xcode 8.2.1 • Swift 3.0.2

您还可以使用 Date 方法 description(with locale: Locale?) 获取用户的本地化时间描述:

A string representation of the Date, using the given locale, or if the locale argument is nil, in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from UTC (for example, “2001-03-24 10:45:32 +0600”).

description(with locale: Locale?)

Date().description(with: .current)   // "Monday, February 9, 2015 at 05:47:51 Brasilia Summer Time"

上面的方法并不意味着在向用户显示日期和时间时使用。它仅用于调试目的。

当向用户显示本地日期和时间(当前时区)时,您应该尊重用户的区域设置和设备设置。您唯一可以控制的是日期和时间样式(短、中、长或完整)。有关更多信息,您可以查看此帖子 shortDateTime .

如果您打算为编码目的创建一个时间戳 UTC (iso8601),您可以查看这篇文章 iso8601

关于ios - Swift - 获取本地日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28404154/

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