gpt4 book ai didi

ios - dateFromComponents 默认为 8 :00 AM

转载 作者:行者123 更新时间:2023-11-28 16:12:18 25 4
gpt4 key购买 nike

<分区>

我试图了解 NSCalendar 及其组件在 Swift 中的工作方式。我从日期中提取组件,更改组件属性,然后重建日期。我期待得到 Sept 9, 2015, 12:00 AM 但是取而代之的是 Sept 9, 2015, 8:00 AM

为什么小时部分是上午 8:00?

let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
calendar?.timeZone = NSTimeZone(abbreviation: "GMT")!

let components = calendar?.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: NSDate())

components?.year // 2016
components?.month // 9
components?.day // 9
components?.hour // 4
components?.minute // 31
components?.second // 54

components?.year = 2015
components?.month = 9
components?.day = 9
components?.hour = 0
components?.minute = 0
components?.second = 0

calendar?.dateFromComponents(components!) // Sept 9, 2015, 8:00 AM

上面的代码并不适用于任何用例。我只是想了解 NSCalendarNSDateComponentsNSDate 是如何链接在一起的。

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