Search code examples
iosobjective-cnsdatenscalendar

Display the current week days and with date


I have a bit of a problem. I want that i display all the days with date in the current week (as i'm writing it's week 38 (Gregorian)). What i have is good but, it doesnt display the correct days. For example: This week the monday is 15th of September and sunday is 21th of September. When i set the the first day of the week it sets it as sunday 14th of September.

NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *currentComps = [myCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:currentDate];
int thisWeeksNumber = currentComps.weekOfYear;
NSLog(@"1  %d", thisWeeksNumber); //This displays the correct week.

[currentComps setWeekday:1];
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:2];
NSDate *secondDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:3];
NSDate *thirdDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:4];
NSDate *fourthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:5];
NSDate *fifthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:6];
NSDate *sixthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:7];
NSDate *seventhDayOfTheWeek = [myCalendar dateFromComponents:currentComps];

NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
myDateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
NSString *secondStr = [myDateFormatter stringFromDate:secondDayOfTheWeek];
NSString *thirdStr = [myDateFormatter stringFromDate:thirdDayOfTheWeek];
NSString *fourthStr = [myDateFormatter stringFromDate:fourthDayOfTheWeek];
NSString *fifthStr = [myDateFormatter stringFromDate:fifthDayOfTheWeek];
NSString *sixthStr = [myDateFormatter stringFromDate:sixthDayOfTheWeek];
NSString *seventhStr = [myDateFormatter stringFromDate:seventhDayOfTheWeek];

NSLog(@"\n %@ \n %@ \n %@ \n %@ \n %@ \n %@ \n %@", firstStr, secondStr, thirdStr, fourthStr, fifthStr, sixthStr, seventhStr);

This should display 15th of september to 21th of september but it displays 14th of september(sunday) to 21th of September(saturday).

Thanks in advance!


Solution

  • If you check the documentation it does exactly what it should do:

    https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateComponents/setWeekday:

    When you call setWeekDay - 1 means Sunday, so if you need to start with Monday your first call should be setWeekDay:2

    Update:

    As for going and getting all consecutive days after the first one I would suggest implementing something like that (insert this function into your code):

    - (NSDate*)dateByAddingDays:(NSInteger)days toDate:(NSDate *)date
    {
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        comps.day = days;
    
        return [calendar dateByAddingComponents: comps toDate: date options: 0];
    }
    

    And use this function to add 1-6 from the day you begin your week. Basically replacing all second and other days code:

    [currentComps setWeekday:2];
    NSDate *secondDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
    

    with the following:

    NSDate *secondDayOfTheWeek = [self dateByAddingDays:1 toDate:firstDayOfTheWeek];
    NSDate *thirdOfTheWeek = [self dateByAddingDays:2 toDate:firstDayOfTheWeek];
    ...