Swift 3.0 Xcode8 beta 4 如何修复 Calendar.current.date
Swift 3.0 Xcode8 beta 4 how to fix Calendar.current.date
晚上,我真的不知道如何转换这个:
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)
有没有更短的路?
谢谢
是的,您可以在使用该初始化程序时仅传递所需的组件以及当前日历,并从 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"
晚上,我真的不知道如何转换这个:
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)
有没有更短的路?
谢谢
是的,您可以在使用该初始化程序时仅传递所需的组件以及当前日历,并从 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"