Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.Value.ToString(format) gives me 12 hour clock

Tags:

c#

An extension of this question, I am pulling a date from a database and displaying it in a grid.

I have the following code:

string date = "";
DateTime? dateSent;    

if (row["DateSent"] != DBNull.Value)
                    dateSent = (DateTime)row["DateSent"];
                else dateSent = null;

date = (dateSent.HasValue ? dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") : null);

When I add a breakpoint at the end of this block of code, I can see that the DateTime? variable "dateSent" has a 24-hour clock timestamp eg 14:50:34. However, when I check the value of the string variable "date" - it has a 12-hour clock format eg 02:50:34.

It is not my intention to convert to a 12 hour clock format. I have two questions:

  1. Why is dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") returning a 12 hour clock timestamp?
  2. How can I avoid this and use the 24-hour clock version?
like image 590
Mike Baxter Avatar asked Apr 05 '13 15:04

Mike Baxter


People also ask

How do I change the date format in ToString?

To do this, you use the "MM/yyyy" format string. The format string uses the current culture's date separator. Getting a string that contains the date and time in a specific format. For example, the "MM/dd/yyyyHH:mm" format string displays the date and time string in a fixed format such as "19//03//2013 18:06".

How do I use 24 hour format in DateTime?

Two 'H' in HH is for 24-hour format. Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.


4 Answers

Why is dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") returning a 12 hour clock timestamp?

Because you're asking for it. That's what hh means. HH is 24-hour.

When in doubt, read the documentation on custom date and time format strings.

hh is documented as

The hour, using a 12-hour clock from 01 to 12."

HH is documented as:

The hour, using a 24-hour clock from 00 to 23.

As a further note, this comment suggests a potential conceptual error:

I can see that the DateTime? variable "dateSent" has a 24-hour clock timestamp eg 14:50:34

A DateTime value doesn't have a format. It's just a date and time. Just as an int with a value of sixteen is both "0x10" and "16" depending on how you format it, the same is true for DateTime. There's no format which goes around with the value - the results of just calling ToString() will depend on the culture settings.

like image 190
Jon Skeet Avatar answered Oct 01 '22 04:10

Jon Skeet


It should be as simple as capitalizing your h's

HH:mm:ss

Here is a decent link to string formating date times

From the article:

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123); 

Notice that the hour is 16

String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
like image 38
Justin Pihony Avatar answered Oct 01 '22 04:10

Justin Pihony


Use capital letters for the Hours:

HH:mm:ss
^^
like image 26
Robert Harvey Avatar answered Oct 01 '22 05:10

Robert Harvey


If you want to display a 24-hours string use the following format string:

HH:mm:ss

Hope it helps

like image 26
Daniel Peñalba Avatar answered Oct 01 '22 06:10

Daniel Peñalba