24

I'm trying to write a simple function using Objective C that accepts an NSDate object and returns an NSDate object that contains the same date value but has removed any time components from the date.

For example if I were to pass an NSDate with a value of '2010-10-12 09:29:34' the function would return 2010-10-12 00:00:00.

The I'm using function seems to work properly. However, when I test the iPhone app I'm developing with Instruments it reports there is a memory leak in the function I'm using. Take a look at my function below. Where is there a memory leak? Is there a better way to achieve the functionality I desire?

Thanks in advance!!

-(NSDate *)dateWithOutTime:(NSDate *)datDate
{
    if (datDate == nil)
    {
        datDate = [NSDate date];
    }

    unsigned int      intFlags   = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSCalendar       *calendar   = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];

     components = [calendar components:intFlags fromDate:datDate];

     return [calendar dateFromComponents:components];
}
Adam Oberhausen
  • 341
  • 1
  • 2
  • 4
  • It's worth noting that NSDate itself has no notion of components as it is an object that represents a point in time. A human readable date has no inherent meaning without time and locale. Even if time is implicitly some time during that date in that locale. – uchuugaka Apr 13 '14 at 10:19

2 Answers2

64

Try this:

-(NSDate *)dateWithOutTime:(NSDate *)datDate {
    if( datDate == nil ) {
        datDate = [NSDate date];
    }
    NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:datDate];
    return [[NSCalendar currentCalendar] dateFromComponents:comps];
}
Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
aegzorz
  • 2,209
  • 15
  • 18
  • Hmmmmmm, the function works. However I still detect the same memory leak when I analyze using Instruments. – Adam Oberhausen Oct 12 '10 at 16:03
  • 1
    That's weird, there should be no memory leaks since all calls are autoreleased. Looking at your original implementation again, there should be no leaks there either. Are you running leaks in the simulator or on a device? – aegzorz Oct 12 '10 at 16:20
  • I'm testing with both simulator and device. After some further investigation I've found that you are correct. There is no memory leak in the function you provided or the original function I was using. The leak is somewhere else in my code. Thanks for assistance. – Adam Oberhausen Oct 12 '10 at 17:13
  • 5
    the name `dateWithOutTime:` is misleading: the time is mid night in the current timezone. – vikingosegundo Feb 05 '13 at 22:11
  • You forgot the timezone component – malhal Dec 05 '13 at 20:15
  • This answer work for me, I fixed NSCalendarUnit deprecated and use it as category. Thank you for your help. – Dody Rachmat Wicaksono Jan 02 '16 at 23:53
14
-(NSDate *)dateWithOutTime:(NSDate *)datDate{
    if( datDate == nil ) {
        datDate = [NSDate date];
    }
    NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:datDate];
    [comps setHour:00];
    [comps setMinute:00];
    [comps setSecond:00];
    [comps setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    return [[NSCalendar currentCalendar] dateFromComponents:comps];
}
Shehbaz Khan
  • 1,892
  • 3
  • 24
  • 31