String to date

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

    String to date

    I've tried several different ways all with the same reults. Invalid date

    Dim ddate As String

    ddate = "11/22/2004"

    Dim TryToConvert As Date = Date.Parse(ddat e)

    Dim TryToConvert2 As Date = Convert.ToDateT ime(ddate)

    MsgBox(CDate(dd ate) & TryToConvert & " " & TryToConvert2)


  • Lucas Tam

    #2
    Re: String to date

    "David Gacek" <sony_dg@hotmai l.com> wrote in news:uTyL3#F0EH A.3468
    @TK2MSFTNGP14.p hx.gbl:
    [color=blue]
    > I've tried several different ways all with the same reults. Invalid date
    >
    > Dim ddate As String
    >
    > ddate = "11/22/2004"
    >
    > Dim TryToConvert As Date = Date.Parse(ddat e)
    >
    > Dim TryToConvert2 As Date = Convert.ToDateT ime(ddate)
    >
    > MsgBox(CDate(dd ate) & TryToConvert & " " & TryToConvert2)[/color]


    Try TryToConvert = CDate("11/22/2004") That should work.


    us/vblr7/html/vagrptypeconver sion.asp


    --
    Lucas Tam (REMOVEnntp@rog ers.com)
    Please delete "REMOVE" from the e-mail address when replying.

    Comment

    • Cor Ligthert

      #3
      Re: String to date

      Lucas,

      For people who are Googling this newsgroup and find your sample,
      [color=blue]
      > Try TryToConvert = CDate("11/22/2004") That should work[/color]
      Try TryToConvert = CDate("11/22/2004") That should work in the USA

      A better sample can be
      \\\
      Dim mydate As String = New Date(2004, 11, 22).ToString
      Dim TryToConvert As Date = CDate(mydate)
      ///

      The last just an idea,

      Cor


      Comment

      • Shiva

        #4
        Re: String to date

        How about DateTime.ParseE xact() ?

        "David Gacek" <sony_dg@hotmai l.com> wrote in message
        news:uTyL3#F0EH A.3468@TK2MSFTN GP14.phx.gbl...
        I've tried several different ways all with the same reults. Invalid date

        Dim ddate As String

        ddate = "11/22/2004"

        Dim TryToConvert As Date = Date.Parse(ddat e)

        Dim TryToConvert2 As Date = Convert.ToDateT ime(ddate)

        MsgBox(CDate(dd ate) & TryToConvert & " " & TryToConvert2)



        Comment

        • David Gacek

          #5
          Re: String to date

          Can you please provide me with a working example
          as you can see this code didn't work either
          Dim ddate As String

          ddate = "11/22/2004"

          Dim TryToConvert As Date

          MsgBox(TryToCon vert = CDate("11/22/2004"))



          "Lucas Tam" <REMOVEnntp@rog ers.com> wrote in message
          news:Xns95A9197 4CF653nntproger scom@140.99.99. 130...[color=blue]
          > "David Gacek" <sony_dg@hotmai l.com> wrote in news:uTyL3#F0EH A.3468
          > @TK2MSFTNGP14.p hx.gbl:
          >[color=green]
          > > I've tried several different ways all with the same reults. Invalid date
          > >
          > > Dim ddate As String
          > >
          > > ddate = "11/22/2004"
          > >
          > > Dim TryToConvert As Date = Date.Parse(ddat e)
          > >
          > > Dim TryToConvert2 As Date = Convert.ToDateT ime(ddate)
          > >
          > > MsgBox(CDate(dd ate) & TryToConvert & " " & TryToConvert2)[/color]
          >
          >
          > Try TryToConvert = CDate("11/22/2004") That should work.
          >
          > http://msdn.microsoft.com/library/de...l=/library/en-
          > us/vblr7/html/vagrptypeconver sion.asp
          >
          >
          > --
          > Lucas Tam (REMOVEnntp@rog ers.com)
          > Please delete "REMOVE" from the e-mail address when replying.
          > http://members.ebay.com/aboutme/coolspot18/[/color]


          Comment

          • Cor Ligthert

            #6
            Re: String to date

            Shiva,
            [color=blue]
            > How about DateTime.ParseE xact() ?[/color]

            That you can use with your sample:

            When it is a field from a textbox, than you have to be absolute sure that
            your program only is used in a USA datetime format zone, by instance for
            Canada that is not sure.

            When it is in a document or webpage when you are 100% sure that it is
            returned from the zone above.

            I hope this helps?

            Cor

            "Shiva" <shiva_sm@onlin e.excite.com>
            [color=blue]
            > How about DateTime.ParseE xact() ?
            >
            > "David Gacek" <sony_dg@hotmai l.com> wrote in message
            > news:uTyL3#F0EH A.3468@TK2MSFTN GP14.phx.gbl...
            > I've tried several different ways all with the same reults. Invalid date
            >
            > Dim ddate As String
            >
            > ddate = "11/22/2004"
            >
            > Dim TryToConvert As Date = Date.Parse(ddat e)
            >
            > Dim TryToConvert2 As Date = Convert.ToDateT ime(ddate)
            >
            > MsgBox(CDate(dd ate) & TryToConvert & " " & TryToConvert2)
            >
            >
            >[/color]


            Comment

            • David Gacek

              #7
              Re: String to date

              DateTime.ParseE xac can i use that in any time zone ? and how can i use it to
              return the date in mmddyyyy format Month/Day/Year ?







              "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
              news:O60YUCH0EH A.2040@tk2msftn gp13.phx.gbl...[color=blue]
              > Shiva,
              >[color=green]
              > > How about DateTime.ParseE xact() ?[/color]
              >
              > That you can use with your sample:
              >
              > When it is a field from a textbox, than you have to be absolute sure that
              > your program only is used in a USA datetime format zone, by instance for
              > Canada that is not sure.
              >
              > When it is in a document or webpage when you are 100% sure that it is
              > returned from the zone above.
              >
              > I hope this helps?
              >
              > Cor
              >
              > "Shiva" <shiva_sm@onlin e.excite.com>
              >[color=green]
              > > How about DateTime.ParseE xact() ?
              > >
              > > "David Gacek" <sony_dg@hotmai l.com> wrote in message
              > > news:uTyL3#F0EH A.3468@TK2MSFTN GP14.phx.gbl...
              > > I've tried several different ways all with the same reults. Invalid date
              > >
              > > Dim ddate As String
              > >
              > > ddate = "11/22/2004"
              > >
              > > Dim TryToConvert As Date = Date.Parse(ddat e)
              > >
              > > Dim TryToConvert2 As Date = Convert.ToDateT ime(ddate)
              > >
              > > MsgBox(CDate(dd ate) & TryToConvert & " " & TryToConvert2)
              > >
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Lucas Tam

                #8
                Re: String to date

                "David Gacek" <sony_dg@hotmai l.com> wrote in
                news:uVUnN4G0EH A.1652@TK2MSFTN GP11.phx.gbl:
                [color=blue]
                > Can you please provide me with a working example
                > as you can see this code didn't work either
                > Dim ddate As String
                >
                > ddate = "11/22/2004"
                >
                > Dim TryToConvert As Date
                >
                > MsgBox(TryToCon vert = CDate("11/22/2004"))[/color]


                Works A-OK on my side...


                Dim DateString As String = "11/22/2004"
                Dim TestDate As Date = CDate(DateStrin g)
                MsgBox(TestDate )

                I don't know what's wrong with your development environment. What error
                did you get?

                Are you in the UK? Possibly CDate is trying to convert your US Date into
                a UK date - which would make it an invalid date.

                --
                Lucas Tam (REMOVEnntp@rog ers.com)
                Please delete "REMOVE" from the e-mail address when replying.

                Comment

                • Shiva

                  #9
                  Re: String to date

                  ParseExact() basically lets you specify the date/time format to expect when
                  parsing the string value.

                  For example, DateTime.ParseE xact ("2004/11/22", "yyyy/MM/dd", null) will get
                  you a DateTime value with correct date value interpretations . And
                  DateTime.ParseE xact ("2004/11/22", "yyyy/MM/dd", null).ToString
                  ("MM/dd/yyyy") returns the value in the specified format - "11/22/2004".

                  Hope this is what you are looking for.

                  "David Gacek" <sony_dg@hotmai l.com> wrote in message
                  news:e1$cQQH0EH A.3976@TK2MSFTN GP09.phx.gbl...
                  DateTime.ParseE xac can i use that in any time zone ? and how can i use it to
                  return the date in mmddyyyy format Month/Day/Year ?







                  "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
                  news:O60YUCH0EH A.2040@tk2msftn gp13.phx.gbl...[color=blue]
                  > Shiva,
                  >[color=green]
                  > > How about DateTime.ParseE xact() ?[/color]
                  >
                  > That you can use with your sample:
                  >
                  > When it is a field from a textbox, than you have to be absolute sure that
                  > your program only is used in a USA datetime format zone, by instance for
                  > Canada that is not sure.
                  >
                  > When it is in a document or webpage when you are 100% sure that it is
                  > returned from the zone above.
                  >
                  > I hope this helps?
                  >
                  > Cor
                  >
                  > "Shiva" <shiva_sm@onlin e.excite.com>
                  >[color=green]
                  > > How about DateTime.ParseE xact() ?
                  > >
                  > > "David Gacek" <sony_dg@hotmai l.com> wrote in message
                  > > news:uTyL3#F0EH A.3468@TK2MSFTN GP14.phx.gbl...
                  > > I've tried several different ways all with the same reults. Invalid date
                  > >
                  > > Dim ddate As String
                  > >
                  > > ddate = "11/22/2004"
                  > >
                  > > Dim TryToConvert As Date = Date.Parse(ddat e)
                  > >
                  > > Dim TryToConvert2 As Date = Convert.ToDateT ime(ddate)
                  > >
                  > > MsgBox(CDate(dd ate) & TryToConvert & " " & TryToConvert2)
                  > >
                  > >
                  > >[/color]
                  >
                  >[/color]



                  Comment

                  • Cor Ligthert

                    #10
                    Re: String to date

                    David,

                    The datetime in Microsoft OS has an internal format in the USA style and not
                    confirm the ISO style (as I wished it was, because that USA style is very
                    confusing outside that culture area).



                    So with string to datetime will the datetime forever in the same way.

                    I hope this helps?

                    Cor


                    Comment

                    • Shiva

                      #11
                      Re: String to date

                      Cor,

                      DateTime.ParseE xact() lets you specifiy the format that the date/time string
                      value is exepcted to be in. If you, for example, have the string as
                      2004/10/20, you can tell DateTime to interpret it as "yyyy/MM/dd" and you
                      get the correct day, month and year values.

                      Of course, an exception is raised if the specified string value is not as
                      per the given format.

                      "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
                      news:O60YUCH0EH A.2040@tk2msftn gp13.phx.gbl...
                      Shiva,
                      [color=blue]
                      > How about DateTime.ParseE xact() ?[/color]

                      That you can use with your sample:

                      When it is a field from a textbox, than you have to be absolute sure that
                      your program only is used in a USA datetime format zone, by instance for
                      Canada that is not sure.

                      When it is in a document or webpage when you are 100% sure that it is
                      returned from the zone above.

                      I hope this helps?

                      Cor

                      "Shiva" <shiva_sm@onlin e.excite.com>
                      [color=blue]
                      > How about DateTime.ParseE xact() ?
                      >
                      > "David Gacek" <sony_dg@hotmai l.com> wrote in message
                      > news:uTyL3#F0EH A.3468@TK2MSFTN GP14.phx.gbl...
                      > I've tried several different ways all with the same reults. Invalid date
                      >
                      > Dim ddate As String
                      >
                      > ddate = "11/22/2004"
                      >
                      > Dim TryToConvert As Date = Date.Parse(ddat e)
                      >
                      > Dim TryToConvert2 As Date = Convert.ToDateT ime(ddate)
                      >
                      > MsgBox(CDate(dd ate) & TryToConvert & " " & TryToConvert2)
                      >
                      >
                      >[/color]



                      Comment

                      • Cor Ligthert

                        #12
                        Re: String to date

                        Shiva,

                        There are long discussions about this in this newsgroup between me and
                        (Herfried and Jay),
                        [color=blue]
                        > Of course, an exception is raised if the specified string value is not as
                        > per the given format.
                        >[/color]
                        Only when the day is above 12 (with leapmonths 13).

                        Cor


                        Comment

                        • David Gacek

                          #13
                          Re: String to date

                          An unhandled exception of type 'System.Invalid CastException' occurred in
                          microsoft.visua lbasic.dll

                          Additional information: Cast from string "11/22/2004" to type 'Date' is not
                          valid.
                          Dim DateString As String = "11/22/2004"

                          '' On the next line i get the error

                          Dim TestDate As Date = CDate(DateStrin g)

                          MsgBox(TestDate )

                          "Lucas Tam" <REMOVEnntp@rog ers.com> wrote in message
                          news:Xns95A92C4 8FC0D7nntproger scom@140.99.99. 130...[color=blue]
                          > "David Gacek" <sony_dg@hotmai l.com> wrote in
                          > news:uVUnN4G0EH A.1652@TK2MSFTN GP11.phx.gbl:
                          >[color=green]
                          > > Can you please provide me with a working example
                          > > as you can see this code didn't work either
                          > > Dim ddate As String
                          > >
                          > > ddate = "11/22/2004"
                          > >
                          > > Dim TryToConvert As Date
                          > >
                          > > MsgBox(TryToCon vert = CDate("11/22/2004"))[/color]
                          >
                          >
                          > Works A-OK on my side...
                          >
                          >
                          > Dim DateString As String = "11/22/2004"
                          > Dim TestDate As Date = CDate(DateStrin g)
                          > MsgBox(TestDate )
                          >
                          > I don't know what's wrong with your development environment. What error
                          > did you get?
                          >
                          > Are you in the UK? Possibly CDate is trying to convert your US Date into
                          > a UK date - which would make it an invalid date.
                          >
                          > --
                          > Lucas Tam (REMOVEnntp@rog ers.com)
                          > Please delete "REMOVE" from the e-mail address when replying.
                          > http://members.ebay.com/aboutme/coolspot18/[/color]


                          Comment

                          • Herfried K. Wagner [MVP]

                            #14
                            Re: String to date

                            "David Gacek" <sony_dg@hotmai l.com> schrieb:[color=blue]
                            > DateTime.ParseE xac can i use that in any time zone ? and how can i use it
                            > to
                            > return the date in mmddyyyy format Month/Day/Year ?[/color]

                            'DateTime.Parse ' is used for the conversion in the 'String' -> 'DateTime'
                            direction.

                            Use 'DateTime.ToStr ing' to convert a 'DateTime' to a string. This method is
                            overloaded and allows you to specify a format:

                            \\\
                            Dim s As String = d.ToString("MMd dyyyy")
                            ///

                            --
                            Herfried K. Wagner [MVP]
                            <URL:http://dotnet.mvps.org/>


                            Comment

                            • Lucas Tam

                              #15
                              Re: String to date

                              "David Gacek" <sony_dg@hotmai l.com> wrote in
                              news:uiZlf2H0EH A.3808@tk2msftn gp13.phx.gbl:
                              [color=blue]
                              > An unhandled exception of type 'System.Invalid CastException' occurred
                              > in microsoft.visua lbasic.dll
                              >
                              > Additional information: Cast from string "11/22/2004" to type 'Date'
                              > is not valid.
                              > Dim DateString As String = "11/22/2004"
                              >
                              > '' On the next line i get the error
                              >
                              > Dim TestDate As Date = CDate(DateStrin g)
                              >
                              > MsgBox(TestDate )[/color]

                              Yup, here is my EXACT code:

                              Dim DateString As String = "11/22/2004"
                              Dim TestDate As Date = CDate(DateStrin g)
                              MsgBox(TestDate = CDate(DateStrin g))

                              In your Regional Settings (Windows Control Panel), what is your date
                              settings set as?



                              --
                              Lucas Tam (REMOVEnntp@rog ers.com)
                              Please delete "REMOVE" from the e-mail address when replying.

                              Comment

                              Working...