gpt4 book ai didi

ios - 日历的 startOfDay() 为某些日期提供了不正确的时间

转载 作者:行者123 更新时间:2023-11-28 23:44:37 25 4
gpt4 key购买 nike

当使用 Date(timeIntervalSince1970: 0) 参数调用 startOfDay(for: ) 时,我得到了一个小时后的日期

例如:

let twoThousandAndOne = Date(timeIntervalSinceReferenceDate: 0)
let nineteenSeventy = Date(timeIntervalSince1970: 0)

print("Two thousand and one: \(twoThousandAndOne)")
print("Nineteen seventy: \(nineteenSeventy)")

let calendar = Calendar.current

print("Start of two thousand and one: \(calendar.startOfDay(for: twoThousandAndOne))")
print("Start of nineteen seventy: \(calendar.startOfDay(for: nineteenSeventy))")

输出:

Two thousand and one: 2001-01-01 00:00:00 +0000
Nineteen seventy: 1970-01-01 00:00:00 +0000
Start of two thousand and one: 2001-01-01 00:00:00 +0000
Start of nineteen seventy: 1969-12-31 23:00:00 +0000

最佳答案

您的时区是 UTC+1

Calendar 考虑本地时区,但 print 显示 UTC 格式的日期。

要打印使用 Calendar 创建的 UTC 日期,请添加 UTC 时区

var calendar = Calendar.current
calendar.timeZone = TimeZone(secondsFromGMT: 0)!

关于ios - 日历的 startOfDay() 为某些日期提供了不正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52839312/

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