Kirti Kesur Kirti Kesur - 1 month ago 7
iOS Question

How to calculate working hours with some task initiation time iOS

Start date and time given. So,


  1. I need to check that if given date is on working day (not on saturday or sunday).

  2. If not then star from next(also check if next day is also not working then start task from next to next day).

  3. The day you start the check that if already other task is allocated or not, If yes in that case start if after that current task.

  4. After that consider the working hours are between 8am to 5pm.

  5. And at the end calculate the task end date and time.


Answer

I think I have done in one of my code, please check if it is helpful to you also

You only need to pass here task start date and time and total hours of task,

/*
 @brife Task end date and time calculation (This calculation is in default timezone)
 */
-(void)calculateDateFromDate:(NSDate *)dtStart Hours:(int)iTotalHours {

    //Total hours given by
    long lTotalSec = iTotalHours * 60 * 60;

    //Non working days
    NSInteger sunday = 1;
    NSInteger saturday = 7;

    // Using a Gregorian calendar.
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    //Temp date object
    NSDate *dtTempTo = dtStart;

    //Final end date for hours calculation
    NSDate *dtEnd = [calendar dateBySettingHour:17 minute:0 second:0 ofDate:dtStart options:0];

    //Loop counter
    int iLoopCount = 0;

    //Check working hourse
    while (lTotalSec > 0) {

        NSDateComponents *dateComponents = [calendar components:NSCalendarUnitWeekday fromDate:dtEnd];

        //Check is working day or not
        if (dateComponents.weekday != saturday && dateComponents.weekday != sunday) {

            //Start date for hours calculation
            NSDate *dtSFHCal;

            //Start date for hours calculation
            if(iLoopCount == 0) {
                dtSFHCal = dtStart;
            } else {
                dtSFHCal = [calendar dateBySettingHour:8 minute:0 second:0 ofDate:dtEnd options:0];
            }
            iLoopCount ++;

            //End date for hours calculation
            NSDate *dtEFHCal = [calendar dateBySettingHour:17 minute:0 second:0 ofDate:dtEnd options:0];

            // Check task start time is between working hours.
            if (([dtTempTo timeIntervalSinceDate:dtSFHCal] >= 0) && ([dtTempTo timeIntervalSinceDate:dtEFHCal] <= 0)) {

                //Check how many hours consume on current date
                long selectedTime = [dtSFHCal timeIntervalSince1970];
                long dayEndTime = [dtEFHCal timeIntervalSince1970];
                long difOfFTime = dayEndTime - selectedTime;
                lTotalSec = lTotalSec - difOfFTime;

                if(lTotalSec > 0) {
                    // If task is not completed in working hours of current date, "Increment" currentDate by 1 day.
                    dtEnd = [calendar dateByAddingComponents:[self dateComponentWithDays:iLoopCount] toDate:dtStart options:0];
                    dtTempTo = [calendar dateBySettingHour:8 minute:0 second:0 ofDate:dtEnd options:0];
                } else {
                    dtEnd = [dtEFHCal dateByAddingTimeInterval:lTotalSec];
                }
            } else {
                // If start time is not in between working hours, "Increment" currentDate by 1 day.
                dtEnd = [calendar dateByAddingComponents:[self dateComponentWithDays:iLoopCount] toDate:dtStart options:0];
                dtTempTo = [calendar dateBySettingHour:8 minute:0 second:0 ofDate:dtEnd options:0];
            }
        } else {
            // If non working day, "Increment" currentDate by 1 day.
            iLoopCount ++;
            dtEnd = [calendar dateByAddingComponents:[self dateComponentWithDays:iLoopCount] toDate:dtStart options:0];
            dtTempTo = [calendar dateBySettingHour:8 minute:0 second:0 ofDate:dtEnd options:0];
        }
    }

    NSLog(@"Task end date/tiem : %@",dtEnd);
}

/*
 @brif Increase "day" in given date
 */
-(NSDateComponents *)dateComponentWithDays:(NSInteger)day {
    NSDateComponents *oneDay = [[NSDateComponents alloc] init];
    [oneDay setDay:day];
    return oneDay;
}