Click here to Skip to main content
15,923,164 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a text box that has a date and time in it formatted as such:

12 Aug 2016 @ 15:00

I need to drop the " @ " and then convert the result to a datetime string. What is proper procedure for this as datetime conversions always throw me?

------

Additionally I have a second area where I have a DateTimePicker (no time) and a 2nd text box with just a time in it.

EX: dpShipArriveDate = 12 Aug 2016 / tbShipArriveTime = 15:28
The above items write to 2 separate data columns in my database and needs to remain so.

I need to concatenate the two items as one datetime string. Currently the tbShipArriveDate puts the date and 00:00:00 and I need to put the time from tbShipArriveTime into the original tbShipArriveDate and replace the 08/12/2016 00:00:00 to end up with a new value of 08/12/2016 15:28:00. The values must be in 24hr time and only HH:mm is required.

My end goal is to compare the two separate datetime values and determine if result2 is AFTER (or later) than result1.

What I have tried:

VB
Private Sub tb01Time_TextChanged(sender As Object, e As EventArgs) Handles tb01Time.TextChanged
        Dim result1 As String = tb01Time.ToString
        Dim result2 As DateTime = DateTime.Parse(dpShipArriveDate.ToString + " " + tbShipArriveTime.ToString)

        Dim late As String

        If result2 > result1 Then
            late = "True"
        Else
            late = "False"
        End If

        Select Case late.ToString
            Case = "True" : tb01Time.BackColor = Color.Red
                tbShipArriveTime.BackColor = Color.Red
                tb01Time.ForeColor = Color.White
                tbShipArriveTime.ForeColor = Color.White
            Case Else : tb01Time.BackColor = Color.White
                tbShipArriveTime.ForeColor = Color.White
                tb01Time.ForeColor = Color.Black
                tbShipArriveTime.ForeColor = Color.Black
        End Select
    End Sub
Posted
Updated 28-Aug-16 12:26pm
v4
Comments
Maciej Los 28-Aug-16 18:03pm    
You cannot compare dates (stored in a string variables) that way!

1 solution

First of all, please read my comment to the question.

You have to convert both strings into single date, then you'll be able to compare it to another date. For example:
VB.NET
Dim sTime As String = "15:00"
Dim sDate As String = "12 Aug 2016"
Dim sDateFormat As String = "dd MMM yyyy HH:mm"
Dim sDateString = String.Concat(sDate, " ", sTime)
Dim resultDate As Date

If Date.TryParseExact(sDateString, sDateFormat, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, resultDate) Then
    Console.WriteLine("'{0}' sucessfuly converted into date!", sDateString)
    'your logic to compare dates!
Else
    Console.WriteLine("Incorrect date: '{0}'", sDateString)
End If


Above code returns:
'12 Aug 2016 15:00' sucessfuly converted into date!

For further details, please see:
DateTime.TryParseExact Method (String, String, IFormatProvider, DateTimeStyles, DateTime) (System)[^]
DateTime.Compare Method (DateTime, DateTime) (System)[^]
DateTime.CompareTo Method (DateTime) (System)[^]
 
Share this answer
 
v2
Comments
K3JAE 28-Aug-16 20:02pm    
Using your code as a template I have successfully worked out the code to compare and correct.

I had to make one change in your code and that was the last line - resultDate as Date ...
Full line now reads: Dim resultDate As Date = CDate(sDateString.ToString)

I also deleted out the ConsoleWrite lines as they were not needed.

Thank you sincerely sir, for your assistance and guidance. Datetime conversions always tear up my brain and just cannot seem to get a handle on them!! Your links provided as well with your solution was also very helpful. Again appreciate the assist.
Maciej Los 29-Aug-16 1:33am    
You're very welcome.
Cheers, Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900