Generate NSDate from day, month, and year

I’m trying to generate a NSDate from a month day and year (all in integer format).

Right now my attempt is such:

  • iPhone screen capture for a view
  • Reset array after playing the game?
  • UITableView disable swipe to delete, but still have delete in Edit mode?
  • How do i add 1 day to a NSDate?
  • undo redo issues with CGLayer
  • Not receiving CloudKit push notifications for Custom Record Zone on the Mac
  • NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    NSNumber *day = [dataSource valueForKey:@"day"];
    NSNumber *month = [dataSource valueForKey:@"month"];
    NSNumber *year = [dataSource valueForKey:@"year"];
    [components setDay:[day intValue]];
    [components setMonth:[month intValue]];
    [components setMonth:[year intValue]];
    NSDate *_date = [calendar dateFromComponents:components];
    

    However, _date outputs the following when day = 24, month = 8, year = 2011:

    0168-07-25 04:56:02 +0000

    I must be doing something horribly wrong, but I have no idea what. Anyone know what might be going on?

    3 Solutions Collect From Internet About “Generate NSDate from day, month, and year”

    You have a typo in your code: (last line should be setYear:)

     [components setDay:[day intValue]];
     [components setMonth:[month intValue]];
     [components setYear:[year intValue]];
    

    You are calling setMonth twice, the second time with the value for year. Should be:

    [components setDay:[day intValue]];
    [components setMonth:[month intValue]];
    [components setYear:[year intValue]];
    

    One glaring glitch is:

    [components setMonth:[year intValue]];    //setting month with year!