let year = 2019 let month = 7 let nowDate:Date = Date() let calendar = Calendar.current let date = calendar.date(from: DateComponents(year: year, month: month, day: 1, hour: 12, minute: 0, second: 0)) let weekOfFirstDay = Calendar.current.component(.weekday, from: date!) // sunday = 1, saturday = 7 let toLastDay = DateComponents(month: 1, day: -1) let endOfMonth = calendar.date(byAdding: toLastDay, to: date!) let lastday = calendar.component(.day, from: endOfMonth!) let format = DateFormatter() format.dateFormat = "yyyy-MM-dd HH:mm:ss" let formattedDate = format.string(from: date!) let nowDateFormatted = format.string(from: nowDate) NSLog(formattedDate) NSLog(nowDateFormatted) NSLog("yearMonth = \(year).\(month)") NSLog("firstWeekDay = \(weekOfFirstDay)") NSLog("end of month = \(lastday)")