简体   繁体   English

Swift 3.0 Xcode8 beta 4 如何修复 Calendar.current.date

[英]Swift 3.0 Xcode8 beta 4 how to fix Calendar.current.date

Evening, I really don't know how to convert this:晚上,我真的不知道如何转换:

let birthDay = Calendar.current.date(era: 1, year: year, month: moth, day: day, hour: 0, minute: 0, second: 0, nanosecond: 0)

Into this:进入这个:

Calendar.current.date(from: <DateComponents>)

Any tips?有小费吗?

This is what I did:这就是我所做的:

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)

Is there a shorter way?有没有更短的方法?

Thanks谢谢

Yes you can pass only the components you want when using that initializer, along with the current calendar and get the date property from the datecomponents object:是的,您只能在使用该初始化程序时传递您想要的组件以及当前日历并从 datecomponents 对象获取日期属性:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM