My Solution for DateMonger


#1

main.m

#import <Foundation/Foundation.h>
#import "NSDate+BNRDateConvenience.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSString *passInDate = [NSDate returnsMidnightForYear:2014 forMonth:07 forDay:21];
        NSLog(@"%@", passInDate);
        
    }
    return 0;
}

NSDate+BNRDateConvenience.h


#import <Foundation/Foundation.h>

@interface NSDate (BNRDateConvenience)

+ (NSString *) returnsMidnightForYear:(int)year forMonth:(int) month forDay:(int) day;

@end

NSDate+BNRDateConvenience.m

#import "NSDate+BNRDateConvenience.h"

// Helper function to display the local date and time.
NSString *localDateAndTime(NSDateComponents *passedInComps)
{
    
    // Use date formatters to fix the timezone issue and displays the correct time
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setTimeStyle:NSDateFormatterFullStyle];
    [formatter setDateStyle:NSDateFormatterFullStyle];
    
    NSCalendar *cal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDate *correctDateandTime = [[NSDate alloc]init];
    correctDateandTime = [cal dateFromComponents:passedInComps];
    
    return ([formatter stringFromDate:correctDateandTime]);
}

@implementation NSDate (BNRDateConvenience)

+ (NSString *) returnsMidnightForYear:(int)year forMonth:(int) month forDay:(int) day;
{

    NSDateComponents *dateComps = [[NSDateComponents alloc]init];
    [dateComps setYear:year];
    [dateComps setMonth:month];
    [dateComps setDay:day];
    [dateComps setHour:00];
    [dateComps setMinute:00];
    [dateComps setSecond:00];
  
    ;
    return (localDateAndTime(dateComps));
    
}
@end