晚上,我真的不知道如何转换:
let birthDay = Calendar.current.date(era: 1, year: year, month: moth, day: day, hour: 0, minute: 0, second: 0, nanosecond: 0)
进入这个:
Calendar.current.date(from: <DateComponents>)
有什么建议吗?
这就是我所做的:
let dateComponet = DateComponents(timeZone: nil, era: 1, year: year, month: month, day: day, hour: 0, minute: 0, second: 0, nanosecond: 0)
let birthDay = Calendar.current.date(from: dateComponet)
有没有更短的方法?
谢谢
Best Answer-推荐答案 strong>
是的,您可以在使用该初始化程序时仅传递您想要的组件,以及当前日历并从 datecomponents 对象获取日期属性:
日期组件
public init(calendar: Calendar? = default,
timeZone: TimeZone? = default,
era: Int? = default,
year: Int? = default,
month: Int? = default,
day: Int? = default,
hour: Int? = default,
minute: Int? = default,
second: Int? = default,
nanosecond: Int? = default,
weekday: Int? = default,
weekdayOrdinal: Int? = default,
quarter: Int? = default,
weekOfMonth: Int? = default,
weekOfYear: Int? = default,
yearForWeekOfYear: Int? = default)
let birthDay = DateComponents(calendar: .current, year: 2016, month: 8, day: 9).date! // "Aug 9, 2016, 12:00 AM"
关于ios - Swift 3.0 Xcode8 beta 4 如何修复 Calendar.current.date,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/38837892/
|