Convert NSDate to String with strftime

让人想犯罪 __ 提交于 2019-12-22 05:42:14

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/5008876/convert-nsdate-to-string-with-strftime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!