Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all the date of the monday of current month in ios sdk

Tags:

ios

iphone

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
like image 810
ios developer Avatar asked Mar 18 '23 22:03

ios developer


2 Answers

//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);
like image 200
Paresh Navadiya Avatar answered Apr 29 '23 02:04

Paresh Navadiya


The basic steps

  1. Create an NSDate object for the first day of that month (e.g., 1/1/2015)
  2. Determine the day of the week for that date
  3. Offset the day to the day of week you are interested in
  4. Add 7 to the day until you reach the end of the month

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]];
like image 20
0x141E Avatar answered Apr 29 '23 02:04

0x141E