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

时间:2018-10-16 15:44:26

标签: ios date

使用参数startOfDay(for: )调用Date(timeIntervalSince1970: 0)时,我得到一个小时的约会

例如:

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

1 个答案:

答案 0 :(得分:1)

您的时区为UTC + 1

v2T{考虑本地时区,但是o以UTC显示日期。

要在UTC中打印用vi{创建的日期,请添加UTC时区

Calendar