How to get all the date of all the monday in current month in ios sdk?
For example i want date of all the monday occur in January-2015
Below code give me month,day and year from nsdate. But now i want nsdate of weekday(Monday) in that month.
NSDate *currentDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate]; // Get necessary date components
 [components month]; //gives you month
 [components day]; //gives you day
 [components year]; // gives you year
//Set Wantedday here with sun=1 ..... sat=7;
NSInteger wantedWeekDay = 2; //for monday
//set current date here
NSDate *currentDate = [NSDate date];
//get calender
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Start out by getting just the year, month and day components of the current date.
NSDateComponents *components = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSCalendarUnitWeekday fromDate:currentDate];
// Change the Day component to 1 (for the first day of the month), and zero out the time components.
[components setDay:1];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
//get first day of current month
NSDate *firstDateOfCurMonth = [gregorianCalendar dateFromComponents:components];
//create new component to get weekday of first date
NSDateComponents *newcomponents = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSCalendarUnitWeekday fromDate:firstDateOfCurMonth];
NSInteger firstDateWeekDay = newcomponents.weekday;
NSLog(@"weekday : %li",(long)firstDateWeekDay);
//get last month date
NSInteger curMonth = newcomponents.month;
[newcomponents setMonth:curMonth+1];
NSDate * templastDateOfCurMonth = [[gregorianCalendar dateFromComponents:newcomponents] dateByAddingTimeInterval: -1]; // One second before the start of next month
NSDateComponents *lastcomponents = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSCalendarUnitWeekday fromDate:templastDateOfCurMonth];
[lastcomponents setHour:0];
[lastcomponents setMinute:0];
[lastcomponents setSecond:0];
NSDate *lastDateOfCurMonth = [gregorianCalendar dateFromComponents:lastcomponents];
NSLog(@"%@",lastDateOfCurMonth);
NSMutableArray *mutArrDates = [NSMutableArray array];
NSDateComponents *dayDifference = [NSDateComponents new];
[dayDifference setCalendar:gregorianCalendar];
//get wanted weekday date
NSDate *firstWeekDateOfCurMonth = nil;
if (wantedWeekDay == firstDateWeekDay) {
    firstWeekDateOfCurMonth = firstDateOfCurMonth;
}
else
{
    NSInteger day = wantedWeekDay - firstDateWeekDay;
    if (day < 0)
        day += 7;
    ++day;
    [components setDay:day];
    firstWeekDateOfCurMonth = [gregorianCalendar dateFromComponents:components];
}
NSLog(@"%@",firstWeekDateOfCurMonth);
NSUInteger weekOffset = 0;
NSDate *nextDate = firstWeekDateOfCurMonth;
do {
    [mutArrDates addObject:nextDate];
    [dayDifference setWeekOfYear:++weekOffset];
    NSDate *date = [gregorianCalendar dateByAddingComponents:dayDifference toDate:firstWeekDateOfCurMonth options:0];
    nextDate = date;
} while([nextDate compare:lastDateOfCurMonth] == NSOrderedAscending || [nextDate compare:lastDateOfCurMonth] == NSOrderedSame);
NSLog(@"%@",mutArrDates);
The basic steps
Here's an example of how to do that
- (NSArray *) datesForWeekday:(NSInteger)weekday forMonth:(NSInteger)month andYear:(NSInteger)year
{
    unsigned int units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay
                        | NSCalendarUnitWeekday;
    // Step 1. create an NSDate for the first of the month
    NSDate *date = [self dateWithMonth:month day:1 andYear:year];
    // Step 2. determine the day of the week (1=Sunday, 2=Monday, ..., 7=Saturday
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:date];
    NSInteger firstDayOfMonth = [comps weekday];
    // Step 3. offset so the day is the day of the week you are interested in
    NSInteger day = weekday - firstDayOfMonth;
    if (day < 0)
        day += 7;
    ++day;
    NSMutableArray *array = [NSMutableArray new];
    NSUInteger numberOfDaysInMonth = [self numberOfDaysWithDate:date];
    // Step 4. add 7 to the day until we reach the end of the month
    do {
        // Add NSDate object to array
        [array addObject:[self dateWithMonth:month day:day andYear:year]];
        // or you can optionally add just the day to the array
        // [array addObject:@(day)];
        day += 7;
    } while (day <= numberOfDaysInMonth);
    return array;
}
// Returns an NSDate object for the specified month, day, and year
- (NSDate *) dateWithMonth:(NSInteger)month day:(NSInteger)day andYear:(NSInteger)year
{
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:day];
    [dateComps setMonth:month];
    [dateComps setYear:year];
    [dateComps setHour:0];
    [dateComps setMinute:0];
    return [[NSCalendar currentCalendar] dateFromComponents:dateComps];
}
// Determines the number of days in the month for specified date
- (NSUInteger) numberOfDaysWithDate:(NSDate *)date
{
    NSRange days = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay
                           inUnit:NSCalendarUnitMonth
                          forDate:date];
    return days.length;
}
Here's an example of how find all Mondays in January of 2015
NSArray *dates = [self datesForWeekday:2 forMonth:1 andYear:2015];
or all the Wednesdays in December 2018
NSArray *dates = [self datesForWeekday:4 forMonth:12 andYear:2018];
or all Mondays in the current month
NSDate *date = [NSDate date];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:date];
NSArray *dates = [self datesForWeekday:2 forMonth:[comps month] andYear:[comps year]];
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