Home All Groups Group Topic Archive Search About

Combining a date value with a time value

Author
22 Sep 2006 6:03 PM
Aussie Rules
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

Author
22 Sep 2006 6:23 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"Aussie Rules" <AussieRules@nospam.nospam> wrote in message
news:uZIkkIn3GHA.2420@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
>
>
>
Author
22 Sep 2006 7:29 PM
Aussie Rules
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

Show quoteHide quote
"Marina Levit [MVP]" <someone@nospam.com> wrote in message
news:%23RjmyQn3GHA.1040@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" <AussieRules@nospam.nospam> wrote in message
> news:uZIkkIn3GHA.2420@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
>>
>>
>>
>
>
Author
22 Sep 2006 7:42 PM
Marina Levit [MVP]
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.

Show quoteHide quote
"Aussie Rules" <AussieRules@nospam.nospam> wrote in message
news:eePv24n3GHA.1268@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]" <someone@nospam.com> wrote in message
> news:%23RjmyQn3GHA.1040@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" <AussieRules@nospam.nospam> wrote in message
>> news:uZIkkIn3GHA.2420@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
>>>
>>>
>>>
>>
>>
>
>
Author
23 Sep 2006 4:16 AM
Jay B. Harlow [MVP - Outlook]
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


Show quoteHide quote
"Aussie Rules" <AussieRules@nospam.nospam> wrote in message
news:eePv24n3GHA.1268@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]" <someone@nospam.com> wrote in message
> news:%23RjmyQn3GHA.1040@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" <AussieRules@nospam.nospam> wrote in message
>> news:uZIkkIn3GHA.2420@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
>>>
>>>
>>>
>>
>>
>
>
Author
25 Sep 2006 6:50 AM
Jeffrey Tan[MSFT]
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.SelectedIndex), 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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
27 Sep 2006 2:41 AM
Jay B. Harlow [MVP - Outlook]
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.com> wrote in message
Show quoteHide quote
news:uTO4g7G4GHA.4464@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.SelectedIndex), 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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>