Getting Current Time in string in Custom format in objective c

Sagar R. Kothari picture Sagar R. Kothari · Nov 6, 2009 · Viewed 82.4k times · Source

I want current time in following format in a string.

dd-mm-yyyy HH:MM

How?

Answer

Carl Norum picture Carl Norum · Nov 6, 2009

You want a date formatter. Here's an example:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm"];

NSDate *currentDate = [NSDate date];
NSString *dateString = [formatter stringFromDate:currentDate];