5

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!

3
  • Can't you just check for midnight and change the format string to "M/dd/yyyy"?
    – Polyfun
    Aug 3, 2011 at 13:49
  • 1
    Unless you have a very large amount of data I find it unlikely that you would many if any entries which happened on midnight when rounded to the nearest second. If you do though surely just showing the date does not tell the user enough information to let them know it happened at midnight especially if viewed without context e.g. on it's own
    – Paul C
    Aug 3, 2011 at 14:38
  • It was graphing a very large amount of data. Hundreds of thousands of points. What I want to format is the X axis labels, most of the times they're rounded its from midnight to midnight, but when you zoom in, you need the H:M:S. The microsoft charts package im using allows for a single format string to be entered. Unfortunely, I don't think I can get the format I want that way, and will have to add some custom code. Aug 3, 2011 at 15:45

4 Answers 4

9
DateTime time = DateTime.Now; 
string txt = time.ToString(time == time.Date ? "M/dd/yyyy" : "M/dd/yyyy HH:mm:ss");
1
  • I will prolly end up using this. Its not a perfect solution (the microsoft charts package doesn't allow selective formatting without adding extra code), but I think its the best solution. Thanks! Aug 3, 2011 at 15:46
4
DateTime time = DateTime.Now; 

string format = "";

if (time.Hour == 0) 
{
   format = "M/dd/yyyy";
} 
else 
{
   format = "M/dd/yyyy HH:mm:ss";
}
3
  • 1
    This will hide the time at for example "00:12:34".
    – Carra
    Jul 24, 2012 at 15:11
  • @Carra: That was the point of the original question.. to hide the time at midnight.
    – user195488
    Jul 24, 2012 at 16:15
  • 1
    2 minutes after midnight is not midnight but the time will be hidden it too. I check midnight with "time == time.Date".
    – Carra
    Jul 25, 2012 at 7:10
3

A culture-independent version of the solution proposed by @JDunkerley would be:

DateTime time = DateTime.Now;
string txt = time.ToString(time == time.Date ? "d" : "g");

More about standard date and time format strings here.

0

The proposed solution is not a very good one because it does not take into account user's localized time format, and simply assumes US English.

Here's how it should've been done:

public static string formatDateTimeWithTimeIfNotMidnight(DateTime dt)
{
    //RETURN:
    //      = String for 'dt' that will have time only if it's not midnight

    if (dt != dt.Date)
    {
        //Use time
        return dt.ToString();
    }
    else
    {
        //Only date
        return dt.ToShortDateString();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.