-1

誰かが会社で働いていた年数に基づいて給与を計算するアプリを作っています。ユーザーは2つの日付を入力します。最初の日付は作業を開始した日(たとえば2010年1月12日)で、2番目の日付は終了日(たとえば2012年1月2日)です。これまでのところ、彼が働いた合計日数はわかっていますが、彼が働いたことを計算するために、より具体的にしたいと思います。

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

日付とカレンダーを扱うのは初めてですが、ご想像のとおり、私はそれらにまったく慣れていません。

4

1 に答える 1

1

このコードを試してください:

-(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]);
    }

}
于 2013-01-11T13:44:41.967 に答える