kmoloney
asked on
VB.NET: Convert Date string and Time string to DateTime value
I have a web app developed in Visual Studio (vb.net) that ties to a SQL Server database. One of the fields in the database is a datetime field. The form has two text boxes, txtDate, and txtTime. txtDate takes a regular expression along standard m/d/yyyy format (e.g., "1/1/2007", "12/13/2007"). txtTime takes a regular expression along standard time notation (e.g., "5:00 PM", "12:00 PM", "4:21 PM").
I need to convert these two separate strings into a datetime object so I can store it in the database.
I know this sounds simple, but I've been having a ton of problems with dates, times, datetime, and timespans. It's not quite intuitive.
Any help?
I need to convert these two separate strings into a datetime object so I can store it in the database.
I know this sounds simple, but I've been having a ton of problems with dates, times, datetime, and timespans. It's not quite intuitive.
Any help?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
6 to one, half dozen to the other.
The two are functionally identical.
You pays your money, and you takes your choice.
Can you say split the points?? LOL ;-}
AW
The two are functionally identical.
You pays your money, and you takes your choice.
Can you say split the points?? LOL ;-}
AW
Just a minor note...
With VB.Net 2005, I would use one of the DateTime.TryParse() methods, instead of DateTime.Parse(), as it allows for easier code to be written:
http://msdn2.microsoft.com/en-us/library/ch92fbc1.aspx
With VB.Net 2005, I would use one of the DateTime.TryParse() methods, instead of DateTime.Parse(), as it allows for easier code to be written:
http://msdn2.microsoft.com/en-us/library/ch92fbc1.aspx
Yes, TryParse is useful if your input format is not validated and you expect failures. Parse defers to TryParse internally anyway and throws an exception if TryParse returns false.
And no, I don't expect any point splitting - it was just a reaction to seeing a lot of questions about converting VB to C#. It's an easier process if you stick to the framework ways of doing things rather than using VB-specific syntax. As AW points out, the net result is the same. In fact, the CType statement ultimately calls DateTie.TryParse as well. Everything ends up in the same happy place.
And no, I don't expect any point splitting - it was just a reaction to seeing a lot of questions about converting VB to C#. It's an easier process if you stick to the framework ways of doing things rather than using VB-specific syntax. As AW points out, the net result is the same. In fact, the CType statement ultimately calls DateTie.TryParse as well. Everything ends up in the same happy place.
ASKER
will this work with the regular expressions for a "DATE" and a "TIME":
DATE regex:
(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01 ])[- /.](19|20)?\d\d
TIME regex:
([0-2])?\d:\d\d [AP]M
I guess I'll try it and find out.
BTW, its up to me if I want to split points... :-)
DATE regex:
(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01
TIME regex:
([0-2])?\d:\d\d [AP]M
I guess I'll try it and find out.
BTW, its up to me if I want to split points... :-)
ASKER
Error received:
Conversion from string "" to type double is not valid.
Conversion from string "" to type double is not valid.
ASKER
Here's my code (if they leave the time field blank, it's assumed to be 5:00 PM)
Private Sub DetermineDateTimeFormat(By Ref strBody As String)
'// DETERMINE THE DATE FORMAT TO BE PRESENT IN THE LETTER
If Me.txtTermTime.Text = "" Then
t.TerminationDateTime = CType(Me.txtTermDate.Text & " " & "5:00 PM", System.DateTime)
Else
t.TerminationDateTime = CType(Me.txtTermDate.Text & " " * Me.txtTermTime.Text, System.DateTime)
End If
End Sub
Private Sub DetermineDateTimeFormat(By
'// DETERMINE THE DATE FORMAT TO BE PRESENT IN THE LETTER
If Me.txtTermTime.Text = "" Then
t.TerminationDateTime = CType(Me.txtTermDate.Text & " " & "5:00 PM", System.DateTime)
Else
t.TerminationDateTime = CType(Me.txtTermDate.Text & " " * Me.txtTermTime.Text, System.DateTime)
End If
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I liked Arthur Wood's solution the best, as IS more VB-ish.
Drichards offered a perfectly valid alternative, and pointed out the typo - something that I should have caught quickly - had I been looking in the write place to begin with. Also, his suggestion re the regex'es, while not really a part of the question, will be useful. I'll probably be using the "IsDate" function to test for it to be a valid date AFTER it matches the regex -- I'm not a regex master by any means, and I'm not planning on trying to come up with one that takes the different days of the month or leap years into accout.
Thanks, Guys.
Kevin
P.S. - it ends up like the points split after all! :-)
Drichards offered a perfectly valid alternative, and pointed out the typo - something that I should have caught quickly - had I been looking in the write place to begin with. Also, his suggestion re the regex'es, while not really a part of the question, will be useful. I'll probably be using the "IsDate" function to test for it to be a valid date AFTER it matches the regex -- I'm not a regex master by any means, and I'm not planning on trying to come up with one that takes the different days of the month or leap years into accout.
Thanks, Guys.
Kevin
P.S. - it ends up like the points split after all! :-)
I always use IsDate(txtString) in a QA step before using the values.
If Not IsDate("User entered stuff") then
... err message
If Not IsDate("User entered stuff") then
... err message
>>>6 to one, half dozen to the other
not really, each has it's own purpose. Doing a conversion is not something you just blankly state doesn't matter. Each way of doing a conversion in .NET has its performance issues, complications, and considerations that you need to be aware of.
not really, each has it's own purpose. Doing a conversion is not something you just blankly state doesn't matter. Each way of doing a conversion in .NET has its performance issues, complications, and considerations that you need to be aware of.
Dim dt As DateTime = DateTime.Parse(txtDate.Tex