Reputation: 25
I have a cell on a DataGridView where I write an hour in this format(hh:mm), but when I try to convert that to a DateTime value it gives me this(01-01-001 00:00:00). Can anyone tell me what I'm doing wrong?
DateTime hora;
hora = Convert.ToDateTime(dataGridView1.Rows[1].Cells[1].Value);
Upvotes: 1
Views: 79
Reputation: 4310
11:23 is not a valid DateTime
, it does not have a Year, Month, and Day as well, That is a TimeSpan
. Use the following:
TimeSpan thetime;
TimeSpan.TryParse(dataGridView1.Rows[1].Cells[1].Value, out thetime);
Upvotes: 0
Reputation: 564383
You likely need to use DateTime.ParseExact
with the appropriate format specifier.
Note that the date portion of the resulting DateTime
will not get set if you use hours/minutes as your format, and will most likely be something you should ignore.
If the hours and minutes just represent a span of time, consider TimeSpan
instead of DateTime
.
Upvotes: 1