Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSDate to String with strftime

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

like image 636
smtlaissezfaire Avatar asked Feb 15 '11 20:02

smtlaissezfaire


2 Answers

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.

like image 92
Matthias Bauch Avatar answered Sep 25 '22 10:09

Matthias Bauch


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

like image 21
paulbailey Avatar answered Sep 22 '22 10:09

paulbailey