Question

I am making an app that calculates salaries based on the years somebody is working for a firm. The user enter 2 dates, the first one is the day he started working (let's say 1/12/2010) and the second is the end date (let's say 1/02/2012). So far i know the total days he worked but i want to be more specific in order to calculate that he worked:

30 days in 2010 * 30$ per day = 90$
365 days in 2011 * 35$ per day = 12775$
32 days in 2012 * 40$ per day = 1280$

It's the first time i am working with dates and calendars and as you can guess i am totally unfamiliar with them.

Was it helpful?

Solution

Try this code :

-(NSInteger)daysBetweenTwoDates:(NSDate *)fromDateTime andDate:(NSDate*)toDateTime{

    NSDate *fromDate;
    NSDate *toDate;

    NSCalendar *calendar = [NSCalendar currentCalendar];

    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:toDateTime];

    NSDateComponents *difference = [calendar components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];

    return [difference day]+1;//+1 as if start and end both date are same, so 1 day worked.
}

- (IBAction)calculate:(id)sender {
    NSDate *startDate=[self.startDate dateValue];//taking from datepicker
    NSDate *endDate=[self.endDate dateValue];//taking from datepicker

    NSTimeZone *gmt=[NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSDateFormatter *dateFormatterYYYY=[NSDateFormatter new];
    [dateFormatterYYYY setTimeZone:gmt];
    [dateFormatterYYYY setDateFormat:@"YYYY"];

    NSDateFormatter *dateFormatterDDMMYYYY=[NSDateFormatter new];
    [dateFormatterDDMMYYYY setDateFormat:@"dd/mm/YYYY"];
    [dateFormatterDDMMYYYY setTimeZone:gmt];


    //1. startDate to 31/12/StartDateYear
    //2. loop to endDateYear
    //3. 01/01/endDateYear to endDate

    /* step 1 */
    //find 31/12/StartDateYear
    NSLog(@"YYYY ; %@",[dateFormatterYYYY stringFromDate:startDate]);

    NSString *lastDateOfStartYearString=[NSString stringWithFormat:@"31/12/%@",[dateFormatterYYYY stringFromDate:startDate]];
    NSDateFormatter *dateFormatddMMyyyy=[NSDateFormatter new];
    [dateFormatddMMyyyy setDateFormat:@"dd'/'MM'/'yyyy"];
    NSDate *lastDateOfStartYear=[dateFormatddMMyyyy dateFromString:lastDateOfStartYearString];

    //no. of days
    NSInteger year=[[dateFormatterYYYY stringFromDate:startDate]integerValue];
    NSInteger days=[self daysBetweenTwoDates:startDate andDate:lastDateOfStartYear];

    //add in dictionary
    NSMutableDictionary *daysForYearDictionary=[NSMutableDictionary new];
    [daysForYearDictionary setValue:@(days) forKey:[NSString stringWithFormat:@"%ld",year]];



    /* step 2*/
    NSInteger nextYearAfterStart=[[dateFormatterYYYY stringFromDate:startDate]integerValue]+1;
    NSInteger previousYearBeforeEnd=[[dateFormatterYYYY stringFromDate:endDate]integerValue]-1;

    for (NSInteger yearCounter=nextYearAfterStart; yearCounter<=previousYearBeforeEnd; yearCounter++) {

        NSString *firstDateString=[NSString stringWithFormat:@"1/1/%ld",yearCounter];
        NSDate *firstDate=[dateFormatddMMyyyy dateFromString:firstDateString];

        NSString *lastDateString=[NSString stringWithFormat:@"31/12/%ld",yearCounter];
        NSDate *lastDate=[dateFormatddMMyyyy dateFromString:lastDateString];

        //no. of days
        days=[self daysBetweenTwoDates:firstDate andDate:lastDate];
        //add in dictionary
        [daysForYearDictionary setValue:@(days) forKey:[NSString stringWithFormat:@"%ld",yearCounter]];
    }



    /* step 3 */
    //find 1/1/EndDateYear
    NSString *firstDateOfStartYearString=[NSString stringWithFormat:@"1/1/%@",[dateFormatterYYYY stringFromDate:endDate]];
    NSDate *firstDateOfEndYear=[dateFormatddMMyyyy dateFromString:firstDateOfStartYearString];

    //no. of days
    year=[[dateFormatterYYYY stringFromDate:endDate]integerValue];
    days=[self daysBetweenTwoDates:firstDateOfEndYear andDate:endDate];
    //add in dictionary
    [daysForYearDictionary setValue:@(days) forKey:[NSString stringWithFormat:@"%ld",year]];




    //printing
    for (NSString *yearStr in daysForYearDictionary) {
        NSLog(@"Year : %@, Days Worked : %@",yearStr,[daysForYearDictionary valueForKey:yearStr]);
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top