date formats

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

    #1

    date formats

    Hi,
    In vb.net, I want to let the user enter a date format in a textbox.
    e.g : mm/dd/yyyy
    dd/mm/yyyy
    dd-mm-yyyy

    Then, how can I check this is a valid date format and how can I build
    an example so that it is displayed in another textbox ?

    12/31/2005

    Regards

  • Ken Tucker [MVP]

    #2
    Re: date formats

    Hi,

    Take a look at the datetime parse and parseexact methods

    http://msdn.microsoft.com/library/de...parsetopic.asp

    http://msdn.microsoft.com/library/de...exacttopic.asp

    Ken
    -------------------------
    "Sam" <samuel.berthel ot@voila.fr> wrote in message
    news:1117621293 .418433.290040@ z14g2000cwz.goo glegroups.com.. .
    Hi,
    In vb.net, I want to let the user enter a date format in a textbox.
    e.g : mm/dd/yyyy
    dd/mm/yyyy
    dd-mm-yyyy

    Then, how can I check this is a valid date format and how can I build
    an example so that it is displayed in another textbox ?

    12/31/2005

    Regards


    Comment

    • Sam

      #3
      Re: date formats

      Thanks but it's not exactely what I want.
      Again I want to check that the string entered by the user (e.g
      DD/MM/yyyy) is a valid date format and then prove it by displaying an
      example (14/12/2005)

      Comment

      • Cor Ligthert

        #4
        Re: date formats

        Sam,

        Normally should the IsDate function be enough

        Cor


        Comment

        • Ken Tucker [MVP]

          #5
          Re: date formats

          Hi,

          You could use a regex to check that it is a valid date format or
          use a try catch block


          Regex

          Dim rValid As New System.Text.Reg ularExpressions .Regex("(0[1-9]|1[012])[-
          /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d")

          If rValid.IsMatch( TextBox1.Text) Then

          Dim dt As DateTime

          dt = DateTime.Parse( TextBox1.Text)

          TextBox2.Text = dt.ToLongDateSt ring

          Else

          MessageBox.Show ("Invalid date format")

          End If




          Try catch block

          Try

          Dim dt As DateTime

          dt = DateTime.Parse( TextBox1.Text)

          TextBox2.Text = dt.ToLongDateSt ring

          Catch

          MessageBox.Show ("Invalid date format")

          End Try



          Ken

          --------------------

          "Sam" <samuel.berthel ot@voila.fr> wrote in message
          news:1117622372 .803107.123520@ g14g2000cwa.goo glegroups.com.. .

          Thanks but it's not exactely what I want.
          Again I want to check that the string entered by the user (e.g
          DD/MM/yyyy) is a valid date format and then prove it by displaying an
          example (14/12/2005)


          Comment

          • Cor Ligthert

            #6
            Re: date formats

            Ken
            [color=blue]
            > You could use a regex to check that it is a valid date format
            > or
            > use a try catch block
            >[/color]
            In my opinion that is impossible the standard IsDate is very fine for that.

            With using a regex you would allow in the first two 0-31 or test that
            31-12 is allowed however
            12-31 not

            However the big problem is that you cannot do anything with that date. How
            do you know what is
            1-12-2005 without culture/language info info.

            For you it is january 12 for me it is 1 december.

            Cor


            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: date formats

              "Sam" <samuel.berthel ot@voila.fr> schrieb:[color=blue]
              > Again I want to check that the string entered by the user (e.g
              > DD/MM/yyyy) is a valid date format and then prove it by displaying an
              > example (14/12/2005)[/color]

              I doubt that there is a way to check if a string is a valid date format,
              because date format strings can contain arbitrary characters.

              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://classicvb.org/petition/>

              Comment

              • Sam

                #8
                Re: date formats

                I haven't tried IsDate yet, but I have tried Ken's idea with the try
                catch block :

                Try
                Dim dt As DateTime
                dt = DateTime.Parse( "dddd - d - MMMM")
                TextBox1.Text = dt.ToLongDateSt ring
                Catch
                MessageBox.Show ("Invalid date format")
                End Try

                However although dddd - d - MMMM is a valid format, it keeps raising an
                exception :
                The string was not recognized as a valid DateTime. There is a unknown
                word starting at index 0.

                Why that ?
                Thx anyway for the replies guys

                Comment

                • Sam

                  #9
                  Re: date formats

                  Cor,
                  I've tried IsDate :

                  ?isdate("dd mmm yyyy")
                  False
                  ?isdate("mm/dd/yyyy")
                  False
                  ?isdate("dd/mm/yyyy")
                  False
                  ?isdate("dd-mm-yyyy")
                  False

                  Can you explain that to me ? Those should be valid date format !

                  Comment

                  • Sam

                    #10
                    Re: date formats

                    damn it.. I posted too quickly. Obviously IsDate expects a date, but
                    then it seems hard to convert the string format entered by the user to
                    a date... I'll try

                    Comment

                    • Sam

                      #11
                      Re: date formats

                      I think I've got it :) Using these function :
                      Note that MyDate.ToString will get what the user entered in my textbox.

                      Dim MyDate As New DateTime(2005, 12, 31)
                      Dim MyString As String = MyDate.ToString ("yyyy-MM-dd")

                      Dim res As Boolean = IsDate(MyString )

                      Thx

                      Comment

                      • Cor Ligthert

                        #12
                        Re: date formats

                        Sam,

                        As I wrote in this thread, the way dates are uses is depending on the
                        culture/language.

                        In Quibec it is the same as with us, however in Edmunton it is as in the
                        USA.

                        However I saw your latter message, therefore I assume you saw that already.

                        Cor


                        Comment

                        • Sam

                          #13
                          Re: date formats

                          Cor,
                          What if I want to check for any kind of regional settings ?

                          For example "MM/dd/yyyy" is not valid on my machine but "dd/MM/yyyy" is
                          valid, and I want both of them to be valid, how ?

                          Thx

                          Comment

                          • Herfried K. Wagner [MVP]

                            #14
                            Re: date formats

                            "Sam" <samuel.berthel ot@voila.fr> schrieb:[color=blue]
                            > I've tried IsDate :
                            >
                            > ?isdate("dd mmm yyyy")
                            > False
                            > ?isdate("mm/dd/yyyy")
                            > False
                            > ?isdate("dd/mm/yyyy")
                            > False
                            > ?isdate("dd-mm-yyyy")
                            > False
                            >
                            > Can you explain that to me ? Those should be valid date format ![/color]

                            'IsDate' is not intended for checking the validity of a date format string.
                            It's used to check the validity of a string representing a certain date.

                            --
                            M S Herfried K. Wagner
                            M V P <URL:http://dotnet.mvps.org/>
                            V B <URL:http://classicvb.org/petition/>

                            Comment

                            • Herfried K. Wagner [MVP]

                              #15
                              Re: date formats

                              "Sam" <samuel.berthel ot@voila.fr> schrieb:[color=blue]
                              > What if I want to check for any kind of regional settings ?
                              >
                              > For example "MM/dd/yyyy" is not valid on my machine but "dd/MM/yyyy" is
                              > valid, and I want both of them to be valid, how ?[/color]

                              AFAIS every string is a valid date format, but an infinite number of date
                              strings won't contain the date in a readable form.

                              --
                              M S Herfried K. Wagner
                              M V P <URL:http://dotnet.mvps.org/>
                              V B <URL:http://classicvb.org/petition/>

                              Comment

                              Working...