gpt4 book ai didi

swift - Swift 中是否只有日期(没有时间)类?

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:18 26 4
gpt4 key购买 nike

Swift 中是否有仅限日期(无时间)的类? (或基础类(class))

(顺便说一句,如果没有,但有一个众所周知/流行的开源库可以实现这个,如果你可以提到这个的话)

最佳答案

Foundation 中没有日期类,但您可以使用 Calendar 从 Date 对象中去除时间。在 Swift 4 中:

func stripTime(from originalDate: Date) -> Date {
let components = Calendar.current.dateComponents([.year, .month, .day], from: originalDate)
let date = Calendar.current.date(from: components)
return date!
}

或者作为扩展:

extension Date {

func stripTime() -> Date {
let components = Calendar.current.dateComponents([.year, .month, .day], from: self)
let date = Calendar.current.date(from: components)
return date!
}

}

关于swift - Swift 中是否只有日期(没有时间)类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35771506/

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