DateTime formatting: Why don't ToShortTimeString and "{0: t}" display the same thing?

var todayAt2PM = new DateTime(
    DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 14, 0, 0);
Debug.Print("{0:t}", todayAt2PM);
Debug.Print("{0}", todayAt2PM.ToString("t"));
Debug.Print("{0}", todayAt2PM.ToShortTimeString());

When I run this in C #, I get:

14:00
2:00 p.m.
2:00 PM

CultureInfo.CurrentCultureand CultureInfo.CurrentUICultureboth are set to "en-US" on my PC. I did not make any settings in my regional settings; when I go to that part of the control panel, everything indicates a 12-hour time with AM and PM.

So why does "{0: t}" print using 24-hour time?

+3
source share
1 answer

I think the problem is with Debug.Print, and not with, the format specifier t. If you use Console.WriteLineinstead Debug.Print, it gives the expected result.

EDIT: Reflector... Debug.Print , , - 24

[Conditional("DEBUG")]
public static void Print(string format, params object[] args)
{
    TraceInternal.WriteLine(string.Format(CultureInfo.InvariantCulture, format, args));
}
+6

All Articles