469,623 Members | 1,683 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Combining a date value with a time value

Hi,

I have a datepicker that show a calender. The user picks a date and the
time component is always 00:00.

I then have a drop down that provides a list of times, (10:00, 11:00 etc),
and I want to combine this with the date value, so that I can store it in a
single field in the database.

How can I combine these two values into one ?

Thanks

Sep 22 '06 #1
6 5350
If it's just times, you can just use the first date object, and call
AddHours to add the number of hours the user selected in the other field.

"Aussie Rules" <Au*********@nospam.nospamwrote in message
news:uZ**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have a datepicker that show a calender. The user picks a date and the
time component is always 00:00.

I then have a drop down that provides a list of times, (10:00, 11:00 etc),
and I want to combine this with the date value, so that I can store it in
a single field in the database.

How can I combine these two values into one ?

Thanks

Sep 22 '06 #2
HI,

Thanks for your reply.

I have tried to apply you suggestion, but have found that the value I am
trying to add to(the time text selected in the drop down) causes a problem
as its a string.

The dropdown contains text values such as (11:00, 11:30).

I get an error as it is trying to convert from a string (11:00) to a type
dbl.

Thanks

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
If it's just times, you can just use the first date object, and call
AddHours to add the number of hours the user selected in the other field.

"Aussie Rules" <Au*********@nospam.nospamwrote in message
news:uZ**************@TK2MSFTNGP02.phx.gbl...
>Hi,

I have a datepicker that show a calender. The user picks a date and the
time component is always 00:00.

I then have a drop down that provides a list of times, (10:00, 11:00
etc), and I want to combine this with the date value, so that I can store
it in a single field in the database.

How can I combine these two values into one ?

Thanks


Sep 22 '06 #3
Right, well, you didn't specify what you were doing exactly.

Once you get the hours in a numeric value, then you can call AddHours. So
your dropdown should have a text and a value. The text can be "10:00 AM",
but the value would be 10. And you add the value.

"Aussie Rules" <Au*********@nospam.nospamwrote in message
news:ee**************@TK2MSFTNGP02.phx.gbl...
HI,

Thanks for your reply.

I have tried to apply you suggestion, but have found that the value I am
trying to add to(the time text selected in the drop down) causes a problem
as its a string.

The dropdown contains text values such as (11:00, 11:30).

I get an error as it is trying to convert from a string (11:00) to a type
dbl.

Thanks

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>If it's just times, you can just use the first date object, and call
AddHours to add the number of hours the user selected in the other field.

"Aussie Rules" <Au*********@nospam.nospamwrote in message
news:uZ**************@TK2MSFTNGP02.phx.gbl...
>>Hi,

I have a datepicker that show a calender. The user picks a date and the
time component is always 00:00.

I then have a drop down that provides a list of times, (10:00, 11:00
etc), and I want to combine this with the date value, so that I can
store it in a single field in the database.

How can I combine these two values into one ?

Thanks



Sep 22 '06 #4
Aussie,
In addition to the other comments, you could convert the string to a
TimeSpan then add the timespan to the Date.

Something like:

Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged,
ComboBox1.SelectedIndexChanged
Label1.Text = CStr(Me.DateTimePicker1.Value.Date +
TimeSpan.Parse(ComboBox1.SelectedValue))
End Sub
I would consider putting TimeSpan objects in my combo box then simply add
them to the Date...

Something like:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
Dim times As New List(Of TimeSpan)
times.Add(New TimeSpan(8, 0, 0))
times.Add(New TimeSpan(8, 30, 0))
times.Add(New TimeSpan(9, 0, 0))
times.Add(New TimeSpan(9, 30, 0))
times.Add(New TimeSpan(10, 0, 0))
times.Add(New TimeSpan(10, 30, 0))
times.Add(New TimeSpan(11, 0, 0))
times.Add(New TimeSpan(11, 30, 0))
Me.ComboBox1.DataSource = times

End Sub

Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged,
ComboBox1.SelectedIndexChanged
Label1.Text = CStr(Me.DateTimePicker1.Value.Date +
DirectCast(ComboBox1.SelectedValue, TimeSpan))
End Sub

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Aussie Rules" <Au*********@nospam.nospamwrote in message
news:ee**************@TK2MSFTNGP02.phx.gbl...
HI,

Thanks for your reply.

I have tried to apply you suggestion, but have found that the value I am
trying to add to(the time text selected in the drop down) causes a problem
as its a string.

The dropdown contains text values such as (11:00, 11:30).

I get an error as it is trying to convert from a string (11:00) to a type
dbl.

Thanks

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>If it's just times, you can just use the first date object, and call
AddHours to add the number of hours the user selected in the other field.

"Aussie Rules" <Au*********@nospam.nospamwrote in message
news:uZ**************@TK2MSFTNGP02.phx.gbl...
>>Hi,

I have a datepicker that show a calender. The user picks a date and the
time component is always 00:00.

I then have a drop down that provides a list of times, (10:00, 11:00
etc), and I want to combine this with the date value, so that I can
store it in a single field in the database.

How can I combine these two values into one ?

Thanks


Sep 23 '06 #5
Hi Aussie,

Thanks for the feedback.

Actually, what Marina suggested is storing the actual TimeSpan value in the
values of the item of Combox, instead of storing it in the text
representation of the item. To use this approach, your item that is added
into Combobox.Items collection should a customized object, which contains
at least 2 properties: text representation and value representation. Below
code snippet demonstrate this approach:

Public Class TimeObject
Private _timeobj As TimeSpan
Public ReadOnly Property TimeText() As String
Get
Return _timeobj.ToString()
End Get
End Property

Public Property TimeValue() As TimeSpan
Get
Return _timeobj
End Get
Set(ByVal value As TimeSpan)
_timeobj = value
End Set
End Property

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer

For i = 0 To 5
Dim item As New TimeObject
item.TimeValue = item.TimeValue.Add(New TimeSpan(i, 0, 0))
Me.ComboBox1.Items.Add(item)
Next
Me.ComboBox1.DisplayMember = "TimeText"
Me.ComboBox1.ValueMember = "TimeValue"
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
Dim selected_obj As TimeObject =
CType(Me.ComboBox1.Items(Me.ComboBox1.SelectedInde x), TimeObject)
Dim ts As TimeSpan = selected_obj.TimeValue
End Sub

Note: while retrieving TimeSpan from database, you should store the time
value in TimeValue property of TimeObject. By using this approach, you may
leverage .Net winform databinding to use the time value.

However, if your design does not want to store time value from database in
a customized TimeObject, you may just add the text representation as the
item to the Combobox items collection. Then you should use TimeSpan.Parse()
to parse the text representation of the item, like this: Dim ts As TimeSpan
= TimeSpan.Parse("11:00")

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 25 '06 #6
Jeffrery,
I suggested using a TimeSpan.

Complete with an example where a custom object is not needed!

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:uT**************@TK2MSFTNGXA01.phx.gbl...
Hi Aussie,

Thanks for the feedback.

Actually, what Marina suggested is storing the actual TimeSpan value in
the
values of the item of Combox, instead of storing it in the text
representation of the item. To use this approach, your item that is added
into Combobox.Items collection should a customized object, which contains
at least 2 properties: text representation and value representation. Below
code snippet demonstrate this approach:

Public Class TimeObject
Private _timeobj As TimeSpan
Public ReadOnly Property TimeText() As String
Get
Return _timeobj.ToString()
End Get
End Property

Public Property TimeValue() As TimeSpan
Get
Return _timeobj
End Get
Set(ByVal value As TimeSpan)
_timeobj = value
End Set
End Property

End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Integer

For i = 0 To 5
Dim item As New TimeObject
item.TimeValue = item.TimeValue.Add(New TimeSpan(i, 0, 0))
Me.ComboBox1.Items.Add(item)
Next
Me.ComboBox1.DisplayMember = "TimeText"
Me.ComboBox1.ValueMember = "TimeValue"
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
Dim selected_obj As TimeObject =
CType(Me.ComboBox1.Items(Me.ComboBox1.SelectedInde x), TimeObject)
Dim ts As TimeSpan = selected_obj.TimeValue
End Sub

Note: while retrieving TimeSpan from database, you should store the time
value in TimeValue property of TimeObject. By using this approach, you may
leverage .Net winform databinding to use the time value.

However, if your design does not want to store time value from database in
a customized TimeObject, you may just add the text representation as the
item to the Combobox items collection. Then you should use
TimeSpan.Parse()
to parse the text representation of the item, like this: Dim ts As
TimeSpan
= TimeSpan.Parse("11:00")

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
Sep 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bruce MacDonald | last post: by
3 posts views Thread by Tom Petersen | last post: by
12 posts views Thread by Assimalyst | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.