I want to display a date with this format :
- MM/dd/yyyy HH:mm:ss tt
for example :
- 01/04/2011 03:34:03 PM
but I have a problem with the following code
class Program
{
static void Main(string[] args)
{
DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 0, 0, 0);
string displayedDate = dt.ToString("MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Console.WriteLine(displayedDate);
Console.Read();
}
}
displays :
01/04/2011 12:00:00 AM
instead of
01/04/2011 00:00:00 AM
Anyone knows why ?
Thank you !
Because you’ve specified a 12-hour clock format instead of a 24-hour format.
The documentation for the
DateTime.ToStringmethod gives a full table of format specifiers and what they mean. The choices for hour are as follows:So, instead of the
hhcustom format specifier, you need to useHH. Change this line:to this: