How to get start date and end date of the current month (Swift 3)
This is what I'm using. Pretty simple but it works.
extension Calendar {
func dayOfWeek(_ date: Date) -> Int {
var dayOfWeek = self.component(.weekday, from: date) + 1 - self.firstWeekday
if dayOfWeek <= 0 {
dayOfWeek += 7
}
return dayOfWeek
}
func startOfWeek(_ date: Date) -> Date {
return self.date(byAdding: DateComponents(day: -self.dayOfWeek(date) + 1), to: date)!
}
func endOfWeek(_ date: Date) -> Date {
return self.date(byAdding: DateComponents(day: 6), to: self.startOfWeek(date))!
}
func startOfMonth(_ date: Date) -> Date {
return self.date(from: self.dateComponents([.year, .month], from: date))!
}
func endOfMonth(_ date: Date) -> Date {
return self.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth(date))!
}
func startOfQuarter(_ date: Date) -> Date {
let quarter = (self.component(.month, from: date) - 1) / 3 + 1
return self.date(from: DateComponents(year: self.component(.year, from: date), month: (quarter - 1) * 3 + 1))!
}
func endOfQuarter(_ date: Date) -> Date {
return self.date(byAdding: DateComponents(month: 3, day: -1), to: self.startOfQuarter(date))!
}
func startOfYear(_ date: Date) -> Date {
return self.date(from: self.dateComponents([.year], from: date))!
}
func endOfYear(_ date: Date) -> Date {
return self.date(from: DateComponents(year: self.component(.year, from: date), month: 12, day: 31))!
}
}
How to use
let calendar: Calendar = Calendar.current
let startDate = calendar.startOfMonth(Date())
print("startDate :: \(startDate)")
You should write this simple code:
let dateFormatter = DateFormatter()
let date = Date()
dateFormatter.dateFormat = "dd-MM-yyyy"
For start Date:
let comp: DateComponents = Calendar.current.dateComponents([.year, .month], from: date)
let startOfMonth = Calendar.current.date(from: comp)!
print(dateFormatter.string(from: startOfMonth))
For end Date:
var comps2 = DateComponents()
comps2.month = 1
comps2.day = -1
let endOfMonth = Calendar.current.date(byAdding: comps2, to: startOfMonth)
print(dateFormatter.string(from: endOfMonth!))