473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 5603
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bruce MacDonald | last post by:
I've got a question/request for the SQL gurus. I'm building a model of bandwidth demand in MS Access and want to get aggregated results for demand at each PCP in each time period. The two...
4
by: Joe User | last post by:
Hi all....I have a feeling this is going to be one of those twisted query questions, but here it goes anyways.... I want to generate a report that shows the chronology of events (represented by...
1
by: PATGMorris | last post by:
I've got a Union Query that pulls data from two different tables for chemistry and micro testing. The tables containing very similar data but for reasons not necessary here, cannot be put into one...
3
by: Tom Petersen | last post by:
Brand new to .aspx, and just modifying code. I have this: writer.WriteLine("DTSTART:" & beginDate.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z") ) I am grabbing the date and time from two...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
7
by: Frank | last post by:
Hi there, I'm trying to generate a report for an old database and I'm having trouble coming up with an elegant way of going about it. Using cursors and other 'ugly' tools I could get the job done...
0
by: Hexman | last post by:
Hello All, I'm stumped with this one. I have a Database table ("PROD") and I have set up 2 different opens for it. As you can see I alias two of the columns in the first open. I run...
7
by: isdeveloper | last post by:
Hi All, I have a problem with a table that I want to get nice data out of in a single query. The guys here reckon it can't be done in a single query but I wanted to prove them wrong !!...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.