Reputation:
I'm setting a date to a certain birthdayTextField like so
@objc func birthdayDatePickerValueChanged(sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
birthdayTextField.text = formatter.string(from: sender.date)
}
Now this textfield value is stored in coredata in a string attribute. There can be many such birthday dates stored in coredata. Now when I fetch these dates from the database, I want to show in a tableview only those dates which come in the following month.
How can it be achieved...?
Upvotes: 0
Views: 105
Reputation: 285150
This is a solution using the powerful date math abilities of Calendar
together with DateComponents
in a Date
extension.
nextDate(after:matching:matchingPolicy:)
looking for day == 1
month
granularity with compare(:to:toGranularity:)
.extension Date {
func isDateInNextMonth() -> Bool {
let calendar = Calendar.current
let nextMonth = calendar.nextDate(after: Date(), matching: DateComponents(day:1), matchingPolicy: .nextTime)!
return calendar.compare(self, to: nextMonth, toGranularity: .month) == .orderedSame
}
}
Use it simply in your method
sender.date.isDateInNextMonth()
Or – more versatile – according to the other isDateIn... methods as extension of Calendar
extension Calendar {
func isDateInNextMonth(_ date : Date) -> Bool {
let nextMonth = self.nextDate(after: Date(), matching: DateComponents(day:1), matchingPolicy: .nextTime)!
return self.compare(date, to: nextMonth, toGranularity: .month) == .orderedSame
}
}
and use it
Calendar.current.isDateInNextMonth(sender.date)
Edit:
If you want to check if the date is in the next 30 days it's still easier
extension Calendar {
func isDateInNextThirtyDays(_ date : Date) -> Bool {
return self.dateComponents([.month], from: Date(), to:date).month! < 1
}
}
Upvotes: 0