I have an array ["24-05-2022", "25-05-2022", "26-05-2022", "27-05-2022", "28-05-2022", "29-05-2022", "30-05-2022"]
, and two buttons as "Next"& "Previous".
So now on clicking next button i need next 7 elements like ["31-05-2022", "01-06-2022", "02-06-2022", "03-06-2022", "04-06-2022", "05-06-2022", "06-06-2022"]
then again clicking next button need next 7 ["07-06-2022 to "13-06-2022"]
elements up to 10 times.
One clicking previous button I need previous 7 elements. So what should be the condition for the 'for loop'? I am getting next dates by using function below.
func getNextDates() {
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-YYYY"
let selectedDate = self.presentDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT 0:00")
guard let selected = dateFormatter.date(from: selectedDate) else {
fatalError()
}
let dateString = formatter.string(from: selected)
print("Selected Date is ==>",dateString)
let calendar = Calendar(identifier: .gregorian)
for i in 1...7 {
let newDate = calendar.date(byAdding: .day, value: i, to: selected)
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-YYYY"
let dateString = formatter.string(from: newDate!)
print("Next DAYS ARE ==>>",dateString)
}
}
I am able to get next dates once. But I want 10 times.
CodePudding user response:
Use the following code by creating an extension to the Date
var daysInCurrentWeek:[Date]?
let formatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
formatter.dateFormat = "dd-MM-YYYY"
daysInCurrentWeek = Date().daysOfWeek()
let strings = daysInCurrentWeek?.map({formatter.string(from: $0)})
print(strings)
}
@IBAction func nextButtonTapped(_ sender: UIButton) {
let nextWeekdays = daysInCurrentWeek?.last?.byAdding(component: .weekOfMonth, value: 1)?.daysOfWeek()
daysInCurrentWeek = nextWeekdays!
print(nextWeekdays?.map({formatter.string(from: $0)}))
}
@IBAction func previousButtonTapped(_ sender: UIButton) {
let previousWeekDays = daysInCurrentWeek?.last?.byAdding(component: .weekOfMonth, value: -1)?.daysOfWeek()
daysInCurrentWeek = previousWeekDays
print(previousWeekDays?.map({formatter.string(from: $0)}))
}
And the date extension will be like this
extension Date {
func byAdding(component: Calendar.Component, value: Int, wrappingComponents: Bool = false, using calendar: Calendar = .current) -> Date? {
calendar.date(byAdding: component, value: value, to: self, wrappingComponents: wrappingComponents)
}
func dateComponents(_ components: Set<Calendar.Component>, using calendar: Calendar = .current) -> DateComponents {
calendar.dateComponents(components, from: self)
}
func startOfWeek(using calendar: Calendar = .current) -> Date {
calendar.date(from: dateComponents([.yearForWeekOfYear, .weekOfYear], using: calendar))!
}
var noon: Date {
Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
func daysOfWeek(using calendar: Calendar = .current) -> [Date] {
let startOfWeek = self.startOfWeek(using: calendar).noon
return (0...6).map { startOfWeek.byAdding(component: .day, value: $0, using: calendar)! }
}
}
Result will be like this
Optional(["12-06-2022", "13-06-2022", "14-06-2022", "15-06-2022", "16-06-2022", "17-06-2022", "18-06-2022"])
Optional(["05-06-2022", "06-06-2022", "07-06-2022", "08-06-2022", "09-06-2022", "10-06-2022", "11-06-2022"])
Optional(["29-05-2022", "30-05-2022", "31-05-2022", "01-06-2022", "02-06-2022", "03-06-2022", "04-06-2022"])
Optional(["05-06-2022", "06-06-2022", "07-06-2022", "08-06-2022", "09-06-2022", "10-06-2022", "11-06-2022"])
Optional(["12-06-2022", "13-06-2022", "14-06-2022", "15-06-2022", "16-06-2022", "17-06-2022", "18-06-2022"])
Optional(["19-06-2022", "20-06-2022", "21-06-2022", "22-06-2022", "23-06-2022", "24-06-2022", "25-06-2022"])
Optional(["26-06-2022", "27-06-2022", "28-06-2022", "29-06-2022", "30-06-2022", "01-07-2022", "02-07-2022"])
Optional(["03-07-2022", "04-07-2022", "05-07-2022", "06-07-2022", "07-07-2022", "08-07-2022", "09-07-2022"])
Optional(["10-07-2022", "11-07-2022", "12-07-2022", "13-07-2022", "14-07-2022", "15-07-2022", "16-07-2022"])