gpt4 book ai didi

swift - UIKit - 日历组件返回负年份的绝对值

转载 作者:行者123 更新时间:2023-12-04 15:13:42 25 4
gpt4 key购买 nike

我在从 Date 中提取 year 时遇到问题。在我使用年份为负数的日期之前,一切正常。然后 UIKit 的 Calendar.componten(:from:) 返回绝对值而不是 1。这是一个例子:

import UIKit

let calendar = Calendar.current

let date = Date(timeIntervalSince1970: -90000000000)
print(date) // -0882-01-14 08:00:00 +0000
print(calendar.component(.year, from: date)) // 883

知道为什么会发生这种情况以及如何在不手动尝试找出年份符号的情况下正确获取日期吗?

谢谢!

最佳答案

负年份应显示为 B.S 9000 而不是“- 9000”。在 Swift 中也有时代实例属性。您可以创建日历并使用这些值例如:

var newCalendar = Calendar(identifier: .gregorian)
newCalendar.timeZone = newCalendarTimeZone

let newCalendarEra = newCalendar.ordinality(of: .day, in: .era, for: now)

关于swift - UIKit - 日历组件返回负年份的绝对值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64730983/

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