Question

How would I convert an NSDate to a NSString, formatted with strftime specifiers?

Was it helpful?

Solution

you could use strftime.

NSDate *date = [NSDate date];
time_t time = [date timeIntervalSince1970];
struct tm timeStruct;
localtime_r(&time, &timeStruct);
char buffer[80];
strftime(buffer, 80, "%d.%m.%Y %H:%M:%S", &timeStruct);
NSString *dateStr = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding];

I hope it's correct.

OTHER TIPS

You'll need an NSDateFormatter, using setDateFormat:. Here's the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top