Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need help the string value come from a textbox.
i cannot use datetimepicker because the textbox consist of other value..

What I have tried:

If ItemBox1.Text <> "" Then
           I1 = "20" & Mid(ItemBox1.Text, 16, 4) & "01"
           Dim I1Date As DateTime = DateTime.ParseExact(I1, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)
           Dim dayApart As TimeSpan = Today.Subtract(I1Date)
           Dim diff As Double = dayApart.TotalDays
           If diff > expiryDate.Text Then
               lblmessage.Text = "*"
           Else
               'do nothing'
           End If
           'do nothing'
       End If
Posted
Updated 5-Oct-17 20:55pm

You should use Date.TryParseExact, below is an example.

Dim enUS As New CultureInfo("en-US") 
      Dim dateString As String
      Dim dateValue As Date

      ' Parse date with no style flags.
      dateString = "20091223"
      If Date.TryParseExact(dateString, "yyyyMMdd", enUS, _
                            DateTimeStyles.None, dateValue) Then
         Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
                           dateValue.Kind)
      Else
         Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
      End If


Refer this for more details.
 
Share this answer
 
v2
Use TryParseExact instead of ParseExact - it returns a boolean value indicating that the parse was ok or bad. If it's bad, report the problem to the user so they can correct it and do not continue.

You should never rely on users to get it right: they make typing mistakes all the time, just like you and I do.
 
Share this answer
 

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