Link to home
Start Free TrialLog in
Avatar of kmoloney
kmoloneyFlag for United States of America

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?
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drichards
drichards

That is correct.  Same thing but a bit more .NET-ish and less VB-ish:

    Dim dt As DateTime = DateTime.Parse(txtDate.Text + " " + txtTime.Text)
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
Avatar of Mike Tomlinson
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
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.
Avatar of kmoloney

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... :-)
Error received:

Conversion from string "" to type double is not valid.
Here's my code (if they leave the time field blank, it's assumed to be 5:00 PM)

    Private Sub DetermineDateTimeFormat(ByRef 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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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! :-)
I always use IsDate(txtString) in a QA step before using the values.

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.