I want current time in following format in a string.
dd-mm-yyyy HH:MM
How?
Thanks in advance.
Sagar
I want current time in following format in a string.
dd-mm-yyyy HH:MM
How?
Thanks in advance.
Sagar
You want a date formatter. Here's an example:
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
[formatter release]; // maybe; you might want to keep the formatter
// if you're doing this a lot.
Either use NSDateFormatter
as Carl said, or just use good old strftime
, which is also perfectly valid Objective-C:
#import <time.h>
time_t currentTime = time(NULL);
struct tm timeStruct;
localtime_r(¤tTime, &timeStruct);
char buffer[20];
strftime(buffer, 20, "%d-%m-%Y %H:%M", &timeStruct);
1> If you are setting dates a lot,
2> you'll want to retain it, us it for everything,
3> then release it in dealloc
Can someone give a code example for #2?