Date to Month, Day, Year in Swift

Written on:July 24, 2014
Comments
Add One

NSDate and NSCalendar is a bit different in Swift. Here is a little bit of code to help you extract the month, day, and year from the current date.


let flags: NSCalendarUnit = .DayCalendarUnit | .MonthCalendarUnit | .YearCalendarUnit
let date = NSDate()
let components = NSCalendar.currentCalendar().components(flags, fromDate: date)

let year = components.year
println(year)
let month = components.month
println(month)
let day = components.day
println(day)

Leave a Comment