How to set time AND date using datepicker?
-
I need a user to provide the date and time. I'm doing this by using two datepicker controls, one to set date and the other one to set time. But the irritating thing is that I can't combine the two results!? All Date and Time properties of a DateTime class are ReadOnly :wtf: (for whatever the reason), but it would be nice if I could just say: UserDate.Date=pickDate.Date UserDate.Hour=pickTime.Hour UserDate.Minute=pickTime.Minute Can anyone help me how to solve this problem? Thank you, Veljko
-
I need a user to provide the date and time. I'm doing this by using two datepicker controls, one to set date and the other one to set time. But the irritating thing is that I can't combine the two results!? All Date and Time properties of a DateTime class are ReadOnly :wtf: (for whatever the reason), but it would be nice if I could just say: UserDate.Date=pickDate.Date UserDate.Hour=pickTime.Hour UserDate.Minute=pickTime.Minute Can anyone help me how to solve this problem? Thank you, Veljko
-
I need a user to provide the date and time. I'm doing this by using two datepicker controls, one to set date and the other one to set time. But the irritating thing is that I can't combine the two results!? All Date and Time properties of a DateTime class are ReadOnly :wtf: (for whatever the reason), but it would be nice if I could just say: UserDate.Date=pickDate.Date UserDate.Hour=pickTime.Hour UserDate.Minute=pickTime.Minute Can anyone help me how to solve this problem? Thank you, Veljko
The DateTime type is a Structure. Structures are value types which are typically immutable in the .NET framework, like the String type. The properties of an immutable types cannot be changed. Instead, the immutable object must be overwritten. Why are they like this? Because Structures are passed by value (a copy of the original Structure). So, changing the properties of a returned structure would only change the copy and not the original. Regarding your DateTimePicker handling, you need to overwrite your UserDate variable. Try something like this...
UserDate = New DateTime(pickDate.Value.Year, pickDate.Value.Month, pickDate.Value.Day, _
pickTime.Value.Hour, pickTime.Value.Minute, pickTime.Value.Second) -
I solved it in the past this way: Dim dtD as DateTime = dtpDate.Value Dim dtT as DateTime = dtpTime.Value Dim dtC as DateTime = dtD.AdTicks(dtT.Ticks) I'm not 100% sure about the code since I go by heart, but I'm 100% sure about the method used.
Briga wrote:
Dim dtD as DateTime = dtpDate.Value Dim dtT as DateTime = dtpTime.Value Dim dtC as DateTime = dtD.AdTicks(dtT.Ticks)
This isn't going to work Briga. Even though the user is only entering time into the "dtpTime" DateTimePicker, this object still internally stores a month/day/year and is included in the returned ticks. So, adding the ticks of both the time and date DateTimePickers will push the resulting DateTime beyond the year 4000. Unless of course you are initializing dtpTime to the beginning of time. See my post below for a better solution.
-
Briga wrote:
Dim dtD as DateTime = dtpDate.Value Dim dtT as DateTime = dtpTime.Value Dim dtC as DateTime = dtD.AdTicks(dtT.Ticks)
This isn't going to work Briga. Even though the user is only entering time into the "dtpTime" DateTimePicker, this object still internally stores a month/day/year and is included in the returned ticks. So, adding the ticks of both the time and date DateTimePickers will push the resulting DateTime beyond the year 4000. Unless of course you are initializing dtpTime to the beginning of time. See my post below for a better solution.
Joshua Quick wrote:
This isn't going to work Briga. Even though the user is only entering time into the "dtpTime" DateTimePicker, this object still internally stores a month/day/year and is included in the returned ticks. So, adding the ticks of both the time and date DateTimePickers will push the resulting DateTime beyond the year 4000. Unless of course you are initializing dtpTime to the beginning of time.
Yeah I believe you as I said I was going by heart. It did work something similar (the idea is by adding the two components). Anyway your solution seems more elegant and efficient than mine.
-
The DateTime type is a Structure. Structures are value types which are typically immutable in the .NET framework, like the String type. The properties of an immutable types cannot be changed. Instead, the immutable object must be overwritten. Why are they like this? Because Structures are passed by value (a copy of the original Structure). So, changing the properties of a returned structure would only change the copy and not the original. Regarding your DateTimePicker handling, you need to overwrite your UserDate variable. Try something like this...
UserDate = New DateTime(pickDate.Value.Year, pickDate.Value.Month, pickDate.Value.Day, _
pickTime.Value.Hour, pickTime.Value.Minute, pickTime.Value.Second)