Andrea Miotto Andrea Miotto - 3 months ago 71
iOS Question

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

Answer

Yes you can pass only the components you want when using that initializer:

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 = Calendar.current.date(from: DateComponents(year: 2016, month: 8, day: 9))   // "Aug 9, 2016, 12:00 AM"
Comments