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 ?
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.
Use CAST SELECT CAST(getdate() AS date); Or you can cast it to varchar: SELECT CAST(getdate() AS varchar(10));
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
NSString *dateString=[dateFormatter stringFromDate:[NSDate date]];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With