Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to get today's date without time [closed]

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 ?

like image 330
Upendar Gareri Avatar asked Aug 08 '13 10:08

Upendar Gareri


People also ask

How can I get current date without time in SQL?

However, if you want to get just the current date – not the date and the time – you can use the CAST() function. This function takes any expression or any column name as the first argument. Then you use the keyword AS and enter the new data type to return.

How do I only get date from Getdate?

Use CAST SELECT CAST(getdate() AS date); Or you can cast it to varchar: SELECT CAST(getdate() AS varchar(10));


2 Answers

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
NSString *dateString=[dateFormatter stringFromDate:[NSDate date]];
like image 144
hitesh Avatar answered Nov 03 '22 01:11

hitesh


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.

like image 45
hashier Avatar answered Nov 03 '22 01:11

hashier