views:

380

answers:

2

I am having problems with adding values to dates and also getting differences between dates. The dates and components calculated are incorrect.

So for adding, if I add 1.5 months, I only get 1 month, however if I add any whole number ie (1 or 2 or 3 and etc) it calculates correctly.

Float32 addAmount = 1.5;

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setMonth:addAmount];

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
 [gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

 NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:Date1 options:0];

Now for difference, if I have a date that has been added with exactly one year (almost same code as above), it adds correctly, but when the difference is calculated, I get 0 years, 11 months and 30 days.

NSDate *startDate = Date1;
   NSDate *endDate = Date2;

   NSCalendar *gregorian = [[NSCalendar alloc]
          initWithCalendarIdentifier:NSGregorianCalendar];
   [gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

   NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

   NSDateComponents *components = [gregorian components:unitFlags
              fromDate:startDate
                toDate:endDate options:0];

   NSInteger years = [components year];
   NSInteger months = [components month];
   NSInteger days = [components day];

What am I doing wrong? Also I have added the kCFCalendarComponentsWrap constanct in the options for both adding and difference functions but with no difference.

Thanks

A: 

So for adding, if I add 1.5 months, I only get 1 month, however if I add any whole number ie (1 or 2 or 3 and etc) it calculates correctly.

The setMonth: method in NSDateComponents takes an NSInteger, not a floating point number. So the behaviour is correct, as it's simply truncating the 1.5 to 1.

Now for difference, if I have a date that has been added with exactly one year (almost same code as above), it adds correctly, but when the difference is calculated, I get 0 years, 11 months and 30 days.

Unless you show the code for how your Date1 and Date2 variables are created, there's no real way to tell.

(You're also leaking memory above; always match an alloc with a release/autorelease. And try not to give your variables capital letters, since as a matter of style, that should only be done for class names)

Shaggy Frog
I didn't notice the memory leak, thanks for spotting it. The date variables are created usually from a uidatepicker with code such as date1 = self.dob.date; Where dob is the datepicker name. Your absolutely right about the variable names though, I changed them for the posting without thinking of case.
Rob
A: 

I finally found the problem with the date difference, when I was saving it to a db, I used timeintervalsince1970 with an double value, but when populating it and setting it to a datepicker, I was using a int column type.

Thanks Shaggy Frog for steering me in the right direction.

Rob