Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have a date time picker. I need to extract only time from it to save to database (DateTime column of my DB)
I am trying to get it through DateTimePicker1.Value.TimeOfDay but this gives error:

Value of type 'System.TimeSpan' cannot be converted to 'Date?'.

Please help.
Thanks
Posted
Updated 16-Sep-13 11:18am
v4
Comments
Richard C Bishop 16-Sep-13 12:34pm    
It's time you format your questions properly. Code blocks do not go around text.

1 solution

You you should understand there is no conversion from time span to date or DateTime. Isn't it logical? Do you understand how DateTime and TimeSpan are different. Imaging someone told you: "In the past, I've spent 90 days learning .NET. How do you think, on what day I stopped to learn it?" You wouldn't be able to answer, would you? :-) So, this part of question makes no sense, let's forget it.

But you don't need that. DateTimePicker actually gives you System.DateTime value, and you can get just the time of the day. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.timeofday.aspx[^].

—SA
 
Share this answer
 
Comments
Furqan Sehgal 16-Sep-13 13:00pm    
I have to give DateTimePicker, user is supposed to chose time.
phil.o 16-Sep-13 13:18pm    
From http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.value.aspx:
DatetimePicker.Value is a property with DateTime type.
So, whether you made a mistake in the error message your wrote, or you wrote some code that you did not show. Anyway it's impossible to guess what's going on with what you provided.
Sergey Alexandrovich Kryukov 16-Sep-13 13:30pm    
Thank you for the note. That's true, but what I've written should still be clear and help to avoid the confusion.
—SA
Sergey Alexandrovich Kryukov 16-Sep-13 13:28pm    
This is not the question you asked in first place. First, will you accept this answer formally (green button)?

As to your comment above, this is not really a question. If you explain the problem and ask your questions, I'll try to answer. And when you say "DateTimePicker", the question would be: which one. Full type name, please. (So far, it wasn't important.)

—SA
Maciej Los 16-Sep-13 13:04pm    
The question is: when you start to learn .NET? :laugh:
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900