5

Currently what I am doing is I am comparing some date say oldDate with today's date

             NSDate *dt = [NSDate date];
             NSComparisonResult result = [oldDate compare:dt];
            if (result == NSOrderedAscending)
            { 
              // do something
            }

dt gives me today's date with time also. But I want just date and no time. How can I achieve it ?

Upendar Gareri
  • 427
  • 1
  • 4
  • 14
  • A lot of answers on stackoverflow: http://stackoverflow.com/questions/1889164/get-nsdate-today-yesterday-this-week-last-week-this-month-last-month-var http://stackoverflow.com/questions/2331129/how-to-determine-if-an-nsdate-is-today – Flop Aug 08 '13 at 10:27

4 Answers4

13
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
NSString *dateString=[dateFormatter stringFromDate:[NSDate date]];
ungato
  • 153
  • 2
  • 12
hitesh
  • 374
  • 1
  • 7
  • But I need to compare two dates here, so by this way if I am converting date_String to nsdate, I am getting the time added to it. – Upendar Gareri Aug 08 '13 at 10:41
  • ok,you have first convert date to string as above code and after you compare two string as.... [String_date1 isEqualToString:String_Date2]; – hitesh Aug 08 '13 at 11:20
4

To take care about timezones and everything you could do something like this:

- (NSInteger)day {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit fromDate:self];
    return [components day];
}

- (NSInteger)month {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit fromDate:self];
    return [components month];
}

- (NSInteger)year {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:self];
    return [components year];
}

Of course you can put it all together in one function just remember to alter components accordingly.

Caveat: If you want something to show to the user, use one of the other answers with NSDateFormatter.

hashier
  • 4,670
  • 1
  • 28
  • 41
2

You need to use a date formatter to correctly output the date and/or time for an NSDate object.

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];

Setting the different NSDateFormatterStyle types will yield more or less information as required, i.e.: "Tuesday, 1st January 2013", or "01/02/2013".

Zack Brown
  • 5,990
  • 2
  • 41
  • 54
  • But how to get date from this dateString without the time added to it. Because when I am converting this dateString to date ( dateFormatter dateFromString:dateString) I am getting time added to it yet again – Upendar Gareri Aug 08 '13 at 10:44
  • By setting `[dateFormatter setTimeStyle:NSDateFormatterNoStyle];` the date formatter will remove the time for you regardless of how you convert the date to a string, or string to a date. – Zack Brown Aug 08 '13 at 10:51
1

Here you go:

NSDate *date = [NSDate Date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.dateFormat = @"HH:mm:ss";
NSString *timeString = [timeFormatter stringFromDate:date];

Now the string timeString contains the current time in HH:mm:ss format, i.e. 11:26:59

Whoops Misread the question! this is for time only for date see other answers - might as well leave this here also.

Woodstock
  • 22,184
  • 15
  • 80
  • 118