views:

5921

answers:

6

Hi there, I was searching here about converting a string like "16:20" to a DateTime type without losing the format, I said I dont want to add dd/MM/yyy or seconds or AM/PM, because db just accept this format.

I tried with Cultures yet

Thanks in Advance

Added Angel says "I am using C#".

+2  A: 

DateTime.Now.ToString("hh:mm") - If it's C#.

Oh. Only read the header.

DateTime dt = new DateTime(2008, 12, 11, Convert.ToInt32("16"), Convert.ToInt32("32"), 0);

MartinHN
capital h's for 24 hour time
John Sheehan
but this convert the value to string right? i want to set it datetime type cause i work with a class that contain an attribute for this value
Angel Escobedo
im getting the value from a gridview In this way:string hour = ((Label)selectedRow.Cells[0].FindControl("lblHorario")).Text;
Angel Escobedo
orDateTime hour = Convert.ToDateTime(((Label)selectedRow.Cells[0].FindControl("lblHorario")).Text);
Angel Escobedo
A: 

I want to address this part of your question:

without losing the format

A database will generally store all datetime values in a standard common format that's not even human readable. If you use a datetime column the original format is destroyed.

However, when you retrieve the value you cast it back to any format you want. If you want HH:mm you can get it.

Joel Coehoorn
I fixed the question so the 'with out loose' part of Joel's comment makes less sense now.
Jonathan Leffler
+1  A: 

what do you mean by "losing the format".

if you convert it to a DateTime type, then the DateTime object will have dd/mm/yy and other properties. depending on how you plan to use the object, you can "recover" your original settings, by formatting the string output like this: DT.ToString("HH:mm");

Victor
+1  A: 

Since you don't stipulate which DBMS you are using, it is hard to know which answer will help you. If you use IBM Informix Dynamic Server, you would simply use the data type 'DATETIME HOUR TO MINUTE', which will record values in the 24 hour clock.

Jonathan Leffler
but in code side im using linqtosql, so i have to pass all data required as DB types.
Angel Escobedo
+1  A: 
DateTime.Parse("16:20")
Jimmy
im getting this with your code snippet:11/12/2008 04:20:00 p.m.
Angel Escobedo
Im Still getting FormatException unhandled by user code, by the way now i set DateTime in both sides, server side and client side, so why stills trowing this exception?
Angel Escobedo
+1  A: 

All DateTime objects must have a date and a time.

If you want just the time, use TimeSpan:

TimeSpan span = TimeSpan.Parse("16:20");

If you want a DateTime, add that time to the min value:

TimeSpan span = TimeSpan.Parse("16.20");
DateTime dt = DateTime.MinValue.Add(span);
// will get you 1/1/1900 4:20 PM which can be formatted with .ToString("HH:mm") for 24 hour formatting
John Sheehan
I will try this, cause i see that the other developer who works whit C++ was storing the data like you specify.
Angel Escobedo
that works but is setting with AM/PM format
Angel Escobedo
is possible to remove the AM/PM at last format? and makes it 24 hours , thanks in advance
Angel Escobedo
DateTime objects don't store dates in a specific format. You can display them however you like. dt.ToString("HH:mm") will get you the 24-hour representation
John Sheehan