That is correct. Same thing but a bit more .NET-ish and less VB-ish:
Dim dt As DateTime = DateTime.Parse(txtDate.Tex
Main Topics
Browse All TopicsI 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?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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
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.
Here's my code (if they leave the time field blank, it's assumed to be 5:00 PM)
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
Not sure what you're doing with the regex, but they are somewhat oversimplified. The time ones are not too bad to improve:
12 hour - probably need some variants on the am/pm:
^(?:[0]?[0-9]|1[0-2]):[0-5
24 hour with two digit hour:
^(?:[0-1][0-9]|[2][0-3]):[
To get the dates correct with the number of days and such is much more complicated. Your simple expressions will validate the formats but won't tell you if the values are legal.
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! :-)
Business Accounts
Answer for Membership
by: Arthur_WoodPosted on 2007-08-04 at 17:45:24ID: 19632490
Dim NewDateTime as DateTime =cType(txtDate.Text & " " & txtTime.Text, DateTime)
txtMyDateTime = NewDateTime
The additional " " is important.
AW