Raz Mahato
Raz Mahato

Reputation: 915

want 24 hr format time in DateTime in C#

My date and time are stored in database in 24 hr format.What i do is use a stored procedure to fetch the required date and times and insert in a datatable. But in the datatable it automatically converts in 12 hr format.After fetching data from datatable it is possible to convert it into 24hr pattern but it has to be converted into string.But the problem is that i need to use the data to create a chart which would only accept DateTime format. So what i want is some way to convert 12 hr time to 24 hr time format without changing it to string.Please help.

Upvotes: 1

Views: 6161

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1500535

My date and time are stored in database in 24 hr format.

I hope they're not actually stored in any text format, but instead in a DateTime field or something similar. Don't confuse "what I see when I run a SQL query" with "what's stored in the database." Just as numbers aren't stored as sequences of decimal digits, dates and times shouldn't be stored as text.

But in the datatable it automatically converts in 12 hr format.

No, if you've done everything properly it should be storing everything in the DataTable as a DateTime. In the debugger you may see a 12-hour string representation, but the object itself should be a DateTime.

Basically the 12/24-hour problem is only a symptom of the real problem: unnecessary string conversions. Track those down (and remove them), and the rest should take care of itself. So if yo're currently calling ToString() when you extract the value from the DataTable, stop doing that. Instead, just cast:

DateTime dateTime = (DateTime) row["foo"];

Upvotes: 9

Thorarin
Thorarin

Reputation: 48486

From your story, I understand that the DataTable actually contains a DateTime value, which doesn't have any format by itself, it basically just stores a number of ticks since an arbitrary date and time. What you are seeing in your table visualization is the default ToString() conversion, based on your application's culture settings.

Most likely, if you were to do something like this before running your existing code, the format would change:

var cultureInfo = new CultureInfo(CultureInfo.CurrentCulture.Name);
cultureInfo.DateTimeFormat.LongTimePattern = "HH:mm:ss";
Thread.CurrentThread.CurrentCulture = cultureInfo;

I would recommend converting to a format of your preference by calling ToString explictly however. How exactly you would best accomplish this depends on how you are visualizing the data from the DataTable.

Upvotes: 2

evgenyl
evgenyl

Reputation: 8107

All that @Jon said is right.

But if you just explained yourself wrong, and you do have DateTime object in your db, and you are looking to format it into 24 hr only when displaying - you can use "HH" in string format for 24 hour format.

For example, for myDateTime representing 16:00:

var hourPart = String.Format("{0:HH}", myDateTime) 
Console.WriteLine(hourPart); // prints 16, and not 4PM.

Upvotes: 0

Related Questions