none
Isdate and Isstring functions RRS feed

  • Question

  • Is there an IsDATE() , ISString() function in VB? I want to check if a value is a date or a string...how should I proceed?

    Sougata Ghosh

    Tuesday, April 24, 2018 5:01 PM

Answers

  • Hi

    Here is some code that might illustrate how to validate a string as a Date ('DateTime' in VB.NET)

    To try it, start a new project and put a TextBox1 and a Button1 on a default Form1 then copy/replace all Form1 code with this.

    When you run it, try out any type of input in the TextBox and then clicking the Button will show a massage if the input is a valid DateTime or not.

    ' Form1 with TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show(GetDate(TextBox1.Text))
      End Sub
      Function GetDate(s As String) As String
        Dim d As DateTime = Nothing
        If Date.TryParse(s, d) Then
          Return "Valid Date (or DateTime)"
        End If
        Return "NOT a Valid Date (or DateTime)"
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by sougata12 Tuesday, April 24, 2018 6:06 PM
    Tuesday, April 24, 2018 5:12 PM

All replies

  • Hi

    Here is some code that might illustrate how to validate a string as a Date ('DateTime' in VB.NET)

    To try it, start a new project and put a TextBox1 and a Button1 on a default Form1 then copy/replace all Form1 code with this.

    When you run it, try out any type of input in the TextBox and then clicking the Button will show a massage if the input is a valid DateTime or not.

    ' Form1 with TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show(GetDate(TextBox1.Text))
      End Sub
      Function GetDate(s As String) As String
        Dim d As DateTime = Nothing
        If Date.TryParse(s, d) Then
          Return "Valid Date (or DateTime)"
        End If
        Return "NOT a Valid Date (or DateTime)"
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by sougata12 Tuesday, April 24, 2018 6:06 PM
    Tuesday, April 24, 2018 5:12 PM
  • there is no single function to do the same?

    Sougata Ghosh

    Tuesday, April 24, 2018 5:26 PM
  • there is no single function to do the same?

    Sougata Ghosh

    Hi

    The 'single function' would be the Date.TryParse. The rest of the example is to illustrate its use. Did you try the example I posted?


    Regards Les, Livingston, Scotland

    Tuesday, April 24, 2018 5:30 PM
  • Hi

    There is an IsDate function:

    Dim bool As Boolean = IsDate(TextBox1.Text)
    but since the TextBox.Text is already a String, AND if valid also a DateTime - how do you want to use both of the functions you mention.


    Regards Les, Livingston, Scotland

    Tuesday, April 24, 2018 5:43 PM
  • my idea is to check if a date converts to a text if a formatting command is applied. For example, I have a date someDate=#2018-04-24# and I write the command :

    newdate= Format(someDate,"DD MMM YYYY")

    I want to check if the value stored in newdate is a string or a date? The reason I want to check this is because I read on a Microsoft page that:

    A standard or custom format string can be used To define the text representation of a date and time value that can be converted to a DateTime or DateTimeOffset value by a parsing operation.


    Sougata Ghosh

    Tuesday, April 24, 2018 5:49 PM
  • my idea is to check if a date converts to a text if a formatting command is applied. For example, I have a date someDate=#2018-04-24# and I write the command :

    newdate= Format(someDate,"DD MMM YYYY")

    I want to check if the value stored in newdate is a string or a date? The reason I want to check this is because I read on a Microsoft page that:

    A standard or custom format string can be used To define the text representation of a date and time value that can be converted to a DateTime or DateTimeOffset value by a parsing operation.


    Sougata Ghosh

    Hi

    I suggest you show variable types in code you post. For example, what type is  'someDate'

    If it is typed as a DateTime then you can easily format in hundreds of ways for display purposes.

    If it a String, then you can convert to a DateTime with the Date.TryParse function that I posted, and then use any of the formatting on it as needed.

    It is usually better to deal with dates/Times in appropriate DateTime typed variable - it makes life so much easier.


    Regards Les, Livingston, Scotland

    Tuesday, April 24, 2018 5:57 PM