Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Sunday, April 22, 2012
Getting Current Time in string in Custom format in objective c
I want current time in following format in a string.
You want a date formatter. Here's an example:
ReplyDeleteNSDateFormatter *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:
ReplyDelete#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);