NSDate / NSCalender计算一年中的天数

时间:2013-01-10 18:49:00

标签: xcode nsdate nscalendar

我正在制作一个应用程序,根据有人为公司工作的年份来计算工资。用户输入2个日期,第一个是他开始工作的那天(比如说2010年1月12日),第二个是结束日期(假设是1/02/2012)。到目前为止,我知道他工作的总天数,但我想更具体,以便计算他的工作时间:

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

这是我第一次使用日期和日历,你可以猜到我完全不熟悉它们。

1 个答案:

答案 0 :(得分: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]);
    }

}