0

Is it possible to print an NSDate object via NSLog without also printing the hour, minute, seconds? What I want it to log is this format yyyy-MM-dd, without the hour, minute, seconds.

I already have an NSDateFormatter with yyyy-MM-dd but when I print the NSDate object via NSLog, it still gives me the hour, minute, second.

Is there a way to do this without creating another string from the date object then logging that string?

4

2 回答 2

1

NSLog遇到一个objective-c对象(通过%@token)时,它调用该方法-[NSObject description]并在其位置打印结果字符串。NSDate的该方法的实现会打印出小时、分钟和秒。让它以不同方式打印的唯一方法是自己使用NSDateFormatter.

于 2013-10-30T23:05:23.183 回答
1

您可以继承 nsdate 并覆盖 description 方法以返回格式化的字符串。或者编写一个类别来做同样的事情,然后在你的日志语句中调用 category 方法。

于 2013-10-31T00:28:10.773 回答