开发者

Proper use of NSDateFormatter?

开发者 https://www.devze.com 2023-02-12 17:08 出处:网络
I\'m trying to get a string with the current date using the following: NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];

I'm trying to get a string with the current date using the following:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSLog(@"%@", [formatter stringFromDate:[NSDate date]]);

However NSLog is n开发者_如何学JAVAot outputting any strings (I'm getting a blank in the console)? What am I doing wrong?


You also have to tell the date formatter the output format you want to use. For example:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSLog(@"%@", [formatter stringFromDate:[NSDate date]]);

See the documentation for more (e.g. setDateFormat:). Sorry for not mentioning this in my other answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号