Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net DateTime formatting hide time if midnight?

I'm working on formatting datetime's to display on a graph, and its working great so far. I have the format string:

M/dd/yyyy HH:mm:ss

and it prints as I'd like it to except the following: I want to completely hide the HH:mm:ss if the time is midnight.

Is it possible to do without a custom iformatprovider?

Thanks!

like image 543
greggorob64 Avatar asked Aug 03 '11 13:08

greggorob64


1 Answers

DateTime time = DateTime.Now; 
string txt = time.ToString(time == time.Date ? "M/dd/yyyy" : "M/dd/yyyy HH:mm:ss");
like image 78
JDunkerley Avatar answered Oct 05 '22 18:10

JDunkerley