Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top