Posts

current date, last date , first date , compared date swift 3

let date = Date () let calendar = Calendar . current let components = calendar . dateComponents ([. year , . month , . day ], from : date ) let year = components . year let month = components . month let day = components . day print ( year ) print ( month ) print ( day ) You get the first day of the month simply with let components = calendar . components ([. Year , . Month ], fromDate : date ) let startOfMonth = calendar . dateFromComponents ( components )! print ( dateFormatter . stringFromDate ( startOfMonth )) // 2015-11-01 To get the last day of the month, add one month and subtract one day: let comps 2 = NSDateComponents () comps 2 . month = 1 comps 2 . day = -1 let endOfMonth = calendar . dateByAddingComponents ( comps 2 , toDate : startOfMonth , options : [])! print ( dateFormatter . stringFromDate ( endOfMonth )) // 2015-11-30 Alternatively, use the  rangeOfUnit  method which gives you the start and the length of th