Simple Conversion from String to Date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phillip Vong

    Simple Conversion from String to Date

    VS2005 in VB.NET

    I have 2 simple textboxes (Textbox1, Textbox2) and they both have dates.

    Textbox1=12/31/2006
    Textbox2=12/15/2006

    I need to convert these two textboxes that are in String format into Date
    format and I'm not sure how. I tried this and I thought it would work, but
    I get the
    Conversion from string "TextBox1" to type 'Date' is not valid.
    -------------------------

    Dim FromDate, ToDate As Date

    FromDate = CType("TextBox1 ", Date)

    ToDate = CType("Textbox2 ", Date)



    Thanks and any help is greatly appreciated.


  • GJH

    #2
    Re: Simple Conversion from String to Date

    it should be this:
    FromDate = CType(TextBox1. Text, Date)



    "Phillip Vong" <phillip_vong*a t*yahoo*dot*com wrote in message
    news:Ob8ePziGHH A.4712@TK2MSFTN GP04.phx.gbl...
    VS2005 in VB.NET
    >
    I have 2 simple textboxes (Textbox1, Textbox2) and they both have dates.
    >
    Textbox1=12/31/2006
    Textbox2=12/15/2006
    >
    I need to convert these two textboxes that are in String format into Date
    format and I'm not sure how. I tried this and I thought it would work,
    but I get the
    Conversion from string "TextBox1" to type 'Date' is not valid.
    -------------------------
    >
    Dim FromDate, ToDate As Date
    >
    FromDate = CType("TextBox1 ", Date)
    >
    ToDate = CType("Textbox2 ", Date)
    >
    >
    >
    Thanks and any help is greatly appreciated.
    >
    >

    Comment

    • Brennan Stehling

      #3
      Re: Simple Conversion from String to Date

      GJH,

      To make it work without ever allowing an exception I do the
      following...

      Dim tmpDate as DateTime = DateTime.MinVal ue

      If (Not DateTime.TryPar se(TextBox1.Tex t, tmpDate)) Then
      ' the date is not valid and the tmp value is still MinValue
      Else
      ' the date was parsed and tmpDate has the value
      End If

      By doing this you can place anything into the TextBox and it will not
      cause an exception.

      Brennan Stehling


      GJH wrote:
      it should be this:
      FromDate = CType(TextBox1. Text, Date)
      >
      >
      >
      "Phillip Vong" <phillip_vong*a t*yahoo*dot*com wrote in message
      news:Ob8ePziGHH A.4712@TK2MSFTN GP04.phx.gbl...
      VS2005 in VB.NET

      I have 2 simple textboxes (Textbox1, Textbox2) and they both have dates.

      Textbox1=12/31/2006
      Textbox2=12/15/2006

      I need to convert these two textboxes that are in String format into Date
      format and I'm not sure how. I tried this and I thought it would work,
      but I get the
      Conversion from string "TextBox1" to type 'Date' is not valid.
      -------------------------

      Dim FromDate, ToDate As Date

      FromDate = CType("TextBox1 ", Date)

      ToDate = CType("Textbox2 ", Date)



      Thanks and any help is greatly appreciated.

      Comment

      • OHM

        #4
        Re: Simple Conversion from String to Date

        Or use the IsDate(MyDateSt ring) function.

        --
        OHM



        "Brennan Stehling" <offwhite@gmail .comwrote in message
        news:1165520731 .553926.162360@ j72g2000cwa.goo glegroups.com.. .
        GJH,
        >
        To make it work without ever allowing an exception I do the
        following...
        >
        Dim tmpDate as DateTime = DateTime.MinVal ue
        >
        If (Not DateTime.TryPar se(TextBox1.Tex t, tmpDate)) Then
        ' the date is not valid and the tmp value is still MinValue
        Else
        ' the date was parsed and tmpDate has the value
        End If
        >
        By doing this you can place anything into the TextBox and it will not
        cause an exception.
        >
        Brennan Stehling

        >
        GJH wrote:
        >it should be this:
        >FromDate = CType(TextBox1. Text, Date)
        >>
        >>
        >>
        >"Phillip Vong" <phillip_vong*a t*yahoo*dot*com wrote in message
        >news:Ob8ePziGH HA.4712@TK2MSFT NGP04.phx.gbl.. .
        VS2005 in VB.NET
        >
        I have 2 simple textboxes (Textbox1, Textbox2) and they both have
        dates.
        >
        Textbox1=12/31/2006
        Textbox2=12/15/2006
        >
        I need to convert these two textboxes that are in String format into
        Date
        format and I'm not sure how. I tried this and I thought it would work,
        but I get the
        Conversion from string "TextBox1" to type 'Date' is not valid.
        -------------------------
        >
        Dim FromDate, ToDate As Date
        >
        FromDate = CType("TextBox1 ", Date)
        >
        ToDate = CType("Textbox2 ", Date)
        >
        >
        >
        Thanks and any help is greatly appreciated.
        >
        >
        >

        Comment

        • Phillip Vong

          #5
          Re: Simple Conversion from String to Date

          Thanks everyone!!!



          "OHM" <lkjhlkjwrote in message
          news:OOPHI9jGHH A.4904@TK2MSFTN GP04.phx.gbl...
          Or use the IsDate(MyDateSt ring) function.
          >
          --
          OHM

          >
          >
          "Brennan Stehling" <offwhite@gmail .comwrote in message
          news:1165520731 .553926.162360@ j72g2000cwa.goo glegroups.com.. .
          >GJH,
          >>
          >To make it work without ever allowing an exception I do the
          >following...
          >>
          >Dim tmpDate as DateTime = DateTime.MinVal ue
          >>
          >If (Not DateTime.TryPar se(TextBox1.Tex t, tmpDate)) Then
          > ' the date is not valid and the tmp value is still MinValue
          >Else
          > ' the date was parsed and tmpDate has the value
          >End If
          >>
          >By doing this you can place anything into the TextBox and it will not
          >cause an exception.
          >>
          >Brennan Stehling
          >http://brennan.offwhite.net/blog/
          >>
          >GJH wrote:
          >>it should be this:
          >>FromDate = CType(TextBox1. Text, Date)
          >>>
          >>>
          >>>
          >>"Phillip Vong" <phillip_vong*a t*yahoo*dot*com wrote in message
          >>news:Ob8ePziG HHA.4712@TK2MSF TNGP04.phx.gbl. ..
          >VS2005 in VB.NET
          >>
          >I have 2 simple textboxes (Textbox1, Textbox2) and they both have
          >dates.
          >>
          >Textbox1=12/31/2006
          >Textbox2=12/15/2006
          >>
          >I need to convert these two textboxes that are in String format into
          >Date
          >format and I'm not sure how. I tried this and I thought it would
          >work,
          >but I get the
          >Conversion from string "TextBox1" to type 'Date' is not valid.
          >-------------------------
          >>
          >Dim FromDate, ToDate As Date
          >>
          >FromDate = CType("TextBox1 ", Date)
          >>
          >ToDate = CType("Textbox2 ", Date)
          >>
          >>
          >>
          >Thanks and any help is greatly appreciated.
          >>
          >>
          >>
          >
          >

          Comment

          Working...