aresz
aresz

Reputation: 2649

Printing Date via NSLog with format

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?

Upvotes: 0

Views: 952

Answers (2)

pickwick
pickwick

Reputation: 3154

You can subclass nsdate and override the description method to return a formatted string. Or write a category to do the same and just call the category method in your log statements.

Upvotes: 1

ianyh
ianyh

Reputation: 619

When NSLog encounters an objective-c object (via the %@ token), it calls the method -[NSObject description] and prints the resulting string in its place. NSDate's implementation of that method prints out hours, minutes, and seconds. The only way to have it print something differently is to generate a string yourself using NSDateFormatter.

Upvotes: 1

Related Questions