i just want an array who contain all the Mondays in the year in the form of NSDate but in swift. i am using the folowing code in the objective -c but dont know how to user it in swift.
NSDate *pickerDate = [NSDate date];
NSLog(@"pickerDate: %@", pickerDate);
NSDateComponents *dateComponents;
NSCalendar *calendar = [NSCalendar currentCalendar];
dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:pickerDate];
NSInteger firstMondayOrdinal = 9 - [dateComponents weekday];
dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:firstMondayOrdinal];
NSDate *firstMondayDate = [calendar dateByAddingComponents:dateComponents toDate:pickerDate options:0];
dateComponents = [[NSDateComponents alloc] init];
[dateComponents setWeek:1];
for (int i=0; i<64; i++) {
[dateComponents setWeek:i];
NSDate *mondayDate = [calendar dateByAddingComponents:dateComponents toDate:firstMondayDate options:0];
NSLog(@"week#: %i, mondayDate: %@", i, mondayDate);
}
Xcode 8 or later • Swift 3 or later
extension Calendar {
static let gregorian = Calendar(identifier: .gregorian)
}
extension Date {
var startOfWeek: Date {
return Calendar.gregorian.date(from: Calendar.gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))!
}
var addingOneWeek: Date {
return Calendar.gregorian.date(byAdding: DateComponents(weekOfYear: 1), to: self)!
}
var nextSunday: Date {
return startOfWeek.addingOneWeek
}
func nextFollowingSundays(_ limit: Int) -> [Date] {
precondition(limit > 0)
var sundays = [nextSunday]
sundays.reserveCapacity(limit)
return [nextSunday] + (0..<limit-1).compactMap { _ in
guard let next = sundays.last?.addingOneWeek else { return nil }
sundays.append(next)
return next
}
}
}
Usage:
let today = Date()
let nextFollowingSundays = today.nextFollowingSundays(52)
nextFollowingSundays.forEach { sunday in
print(sunday.description(with: .current), terminator: "\n")
}
// ["Feb 17, 2019 at 12:00 AM", "Feb 24, 2019 at 12:00 AM", "Mar 3, 2019 at 12:00 AM", "Mar 10, 2019 at 12:00 AM", "Mar 17, 2019 at 12:00 AM", "Mar 24, 2019 at 12:00 AM", "Mar 31, 2019 at 12:00 AM", "Apr 7, 2019 at 12:00 AM", "Apr 14, 2019 at 12:00 AM", "Apr 21, 2019 at 12:00 AM", "Apr 28, 2019 at 12:00 AM", "May 5, 2019 at 12:00 AM", "May 12, 2019 at 12:00 AM", "May 19, 2019 at 12:00 AM", "May 26, 2019 at 12:00 AM", "Jun 2, 2019 at 12:00 AM", "Jun 9, 2019 at 12:00 AM", "Jun 16, 2019 at 12:00 AM", "Jun 23, 2019 at 12:00 AM", "Jun 30, 2019 at 12:00 AM", "Jul 7, 2019 at 12:00 AM", "Jul 14, 2019 at 12:00 AM", "Jul 21, 2019 at 12:00 AM", "Jul 28, 2019 at 12:00 AM", "Aug 4, 2019 at 12:00 AM", "Aug 11, 2019 at 12:00 AM", "Aug 18, 2019 at 12:00 AM", "Aug 25, 2019 at 12:00 AM", "Sep 1, 2019 at 12:00 AM", "Sep 8, 2019 at 12:00 AM", "Sep 15, 2019 at 12:00 AM", "Sep 22, 2019 at 12:00 AM", "Sep 29, 2019 at 12:00 AM", "Oct 6, 2019 at 12:00 AM", "Oct 13, 2019 at 12:00 AM", "Oct 20, 2019 at 12:00 AM", "Oct 27, 2019 at 12:00 AM", "Nov 3, 2019 at 1:00 AM", "Nov 10, 2019 at 1:00 AM", "Nov 17, 2019 at 12:00 AM", "Nov 24, 2019 at 12:00 AM", "Dec 1, 2019 at 12:00 AM", "Dec 8, 2019 at 12:00 AM", "Dec 15, 2019 at 12:00 AM", "Dec 22, 2019 at 12:00 AM", "Dec 29, 2019 at 12:00 AM", "Jan 5, 2020 at 12:00 AM", "Jan 12, 2020 at 12:00 AM", "Jan 19, 2020 at 12:00 AM", "Jan 26, 2020 at 12:00 AM", "Feb 2, 2020 at 12:00 AM", "Feb 9, 2020 at 12:00 AM", "Feb 16, 2020 at 12:00 AM"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With