-1

I'm trying to set the month and year of [NSDate date] but the function dateFromComponents return the wrong value, this is my code:

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSUIntegerMax fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:monthInt];
[components setYear:yearInt];

// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];

yearInt is 2014 and monthInt is 12, but it reports back 12-2015 and not 2014, why is this?

Thanks!

Erik
  • 2,500
  • 6
  • 28
  • 49

1 Answers1

0

you need to put only the bit operator you need, try this:

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear |NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitTimeZone fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:12];
[components setYear:2014];

// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];

NSLog(@"Ver %@",dateToCompare);

NSUIntegerMax = 18446744073709551615 In bit representation: 10000000000000000000000000000000000000000000000000000000000000000 To many switch off.

Onik IV
  • 5,007
  • 2
  • 18
  • 23