gpt4 book ai didi

nsdate - 如何使用 NSDate 在 Swift 3 中获取时间(小时、分钟、秒)?

转载 作者:行者123 更新时间:2023-12-01 07:16:26 25 4
gpt4 key购买 nike

如何从 Swift 3 中的 NSDate 类确定小时、分钟和秒?

在 Swift 2 中:

let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour

swift 3?

最佳答案

swift 3.0 Apple 删除了“NS”前缀并使一切变得简单。以下是从“日期”类(NSDate 替代)中获取小时、分钟和秒的方法

let date = Date()
let calendar = Calendar.current

let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
print("hours = \(hour):\(minutes):\(seconds)")

像这些你可以通过相应的传递来获得时代、年、月、日等。

关于nsdate - 如何使用 NSDate 在 Swift 3 中获取时间(小时、分钟、秒)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38248941/

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