iOSDeveloper
iOSDeveloper

Reputation: 461

Next Week Calculation

I have a requirement in which i have to calculate working days i.e from Monday to Friday. if i pass todays date that particular week start date (Monday's Date) and end date(Friday's Date) will be displayed. And i calculated using the below Code.

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

   [dateFormat setDateFormat:@"yyyy-MM-dd"];// you can use your format.
    todayDate= [NSDate date];

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:todayDate];

    int dayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:todayDate] weekday];// this will give you current day of week

    [components setDay:([components day] - ((dayofweek) - 2))];// for beginning of the week.

    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];

    [dateFormat_first setDateFormat:@"yyyy-MM-dd"];
    NSString * dateString2Prev = [dateFormat stringFromDate:beginningOfWeek];
    NSDate * weekstartPrev = [dateFormat_first dateFromString:dateString2Prev];
   // NSLog(@"The start of the week %@",weekstartPrev);
    startDate= [weekstartPrev dateByAddingTimeInterval:60*60*24*1];
    //NSLog(@"The date plus one is %@", startDate);
    //Week End Date


    NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:todayDate];
    int Enddayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:todayDate] weekday];// this will give you current day of week
    [componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+1)];// for end day of the week
    NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd];
    NSDateFormatter *dateFormat_End = [[NSDateFormatter alloc] init];
    [dateFormat_End setDateFormat:@"yyyy-MM-dd"];
    NSString *dateEndPrev = [dateFormat stringFromDate:EndOfWeek];
    NSDate *weekEndPrev = [dateFormat_End dateFromString:dateEndPrev];


    endDate= [weekEndPrev dateByAddingTimeInterval: -86400.0];
    NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
    [dateF setDateFormat:@"dd-MMM-yyyy"];

    [dateF setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateF setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];

    endDateString = [dateF stringFromDate:endDate];      //pass this date to time table view controller

    NSLog(@"Start date %@ and End date %@ ",startDate, endDate);

    NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
    [dateF setDateFormat:@"dd-MMM-yyyy"];

    [dateF setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateF setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];

    startDateString = [dateF stringFromDate:startDate];   
    NSLog(@"the date start %@ ", startDateString);

Now My question is how can I calculate next week for example todays date is Tue 26 Aug 2014. I calculated the current week i.e from Mon 25 Aug 2014 TO Fri 29 Aug 2014 using the above code. how can I calculate next week i.e from 1st September 2014 (monday) To 5th September 2014 (Friday).

Upvotes: 0

Views: 172

Answers (1)

tdelepine
tdelepine

Reputation: 2016

i have worked on the similar issues some weeks ago.

I created a category on NSDate. You can use the methods.

- (NSDate *)firstDayOfWeek
{
    return [NSDate firstDayOfWeekFor:self];
}

+(NSDate *)firstDayOfWeekFor:(NSDate*)p_Date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:p_Date];
    NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
    if ([weekdayComponents weekday] == 1)
    {
        // dimanche, on soustrait 6 jours
        [componentsToSubtract setDay:-6];
    }
    else
    {
        // autres jours, on soustrait le jour courant et on rajoute 2 (=lundi)
        [componentsToSubtract setDay:(-[weekdayComponents weekday] + 2)];
    }
    return [calendar dateByAddingComponents:componentsToSubtract toDate:[p_Date withoutTime] options:0];
}

- (NSDate *) dateByAddingDays:(NSInteger)p_Days
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:p_Days];
    return [calendar dateByAddingComponents:components toDate:self options:0];
}

Upvotes: 2

Related Questions