Convert a date string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fpvt2@yahoo.com

    #1

    Convert a date string

    In VB6, I can format a date string like the following:
    format("12/15/06","yymmdd") and it returns 061215.
    In VB.Net, when I do the following:
    sdate.tostring( "yymmdd"), it gave me an error "Value of type string
    cannot be converted to 'System.IFormat Provider'

    How can I do it in VB.NET ?
    Thanks.

  • rowe_newsgroups

    #2
    Re: Convert a date string

    AFAIK the .ToString(...da teformat...) only works with a DateTime
    object. Try this instead:

    MessageBox.Show (Convert.ToDate Time("12/15/06").ToString(" yyMMdd"))

    Thanks,

    Seth Rowe


    fpvt2@yahoo.com wrote:
    In VB6, I can format a date string like the following:
    format("12/15/06","yymmdd") and it returns 061215.
    In VB.Net, when I do the following:
    sdate.tostring( "yymmdd"), it gave me an error "Value of type string
    cannot be converted to 'System.IFormat Provider'
    >
    How can I do it in VB.NET ?
    Thanks.

    Comment

    • rowe_newsgroups

      #3
      Re: Convert a date string

      I'm thinking he's using hungarian notation - so sdate is probably
      declared as a string, not a datetime.

      Thanks,

      Seth Rowe


      Kerry Moorman wrote:
      fpvt2,
      >
      I did not have any problems getting the following code to work:
      >
      Dim d1 As DateTime = Now
      >
      Console.WriteLi ne(d1.ToString( "yyMMdd"))
      >
      Maybe you can post the exact code that is giving you a problem?
      >
      Kerry Moorman
      >
      >
      "fpvt2@yahoo.co m" wrote:
      >
      In VB6, I can format a date string like the following:
      format("12/15/06","yymmdd") and it returns 061215.
      In VB.Net, when I do the following:
      sdate.tostring( "yymmdd"), it gave me an error "Value of type string
      cannot be converted to 'System.IFormat Provider'

      How can I do it in VB.NET ?
      Thanks.

      Comment

      • Branco Medeiros

        #4
        Re: Convert a date string


        fpvt2@yahoo.com wrote:
        In VB6, I can format a date string like the following:
        format("12/15/06","yymmdd") and it returns 061215.
        In VB.Net, when I do the following:
        sdate.tostring( "yymmdd"), it gave me an error "Value of type string
        cannot be converted to 'System.IFormat Provider'
        >
        How can I do it in VB.NET ?
        I guess you must convert the string to date first (something VB6 did
        for you, automagically), and *then* format the date the way you want.
        =)

        I mean:

        Dim D As String = _
        Date.ParseExact (sDate, "mm/dd/yy", Nothing).ToStri ng("yymmdd")

        HTH

        Regards,

        Branco.

        Comment

        • Phill W.

          #5
          Re: Convert a date string

          fpvt2@yahoo.com wrote:
          In VB6, I can format a date string like the following:
          format("12/15/06","yymmdd") and it returns 061215.
          In VB.Net, when I do the following:
          sdate.tostring( "yymmdd"), it gave me an error "Value of type string
          cannot be converted to 'System.IFormat Provider'
          That's because VB 'Proper's Evil Type Coersion implicitly changed the
          string value "12/15/06" into a Date value and then formatted /that/.

          Visual Basic forces you to take more care over how things do things.
          Formatting (ToString'ing) a String is very different from the formatting
          a Date:

          ? DateTime.Parse( "12/15/06").ToString ( "yymmdd" )

          Or, since you start and end with Strings:

          sDate.Substring ( 6, 2 ) _
          & sDate.Substring ( 0, 2 ) _
          & sData.Substring ( 3, 2 )

          HTH,
          Phill W.

          Comment

          • Fabio Z

            #6
            Re: Convert a date string

            "rowe_newsgroup s" <rowe_email@yah oo.comha scritto nel messaggio
            AFAIK the .ToString(...da teformat...) only works with a DateTime
            object. Try this instead:
            >
            MessageBox.Show (Convert.ToDate Time("12/15/06").ToString(" yyMMdd"))
            Pay attention with dates as "1/5/2006" where you can't tell if 1 is the day
            or the month.



            Comment

            • fpvt2@yahoo.com

              #7
              Re: Convert a date string

              Thank you, eveybody.
              Convert.ToDateT ime("12/15/06").ToString(" yyMMdd")) does it.

              Thanks.


              Fabio Z wrote:
              "rowe_newsgroup s" <rowe_email@yah oo.comha scritto nel messaggio
              >
              AFAIK the .ToString(...da teformat...) only works with a DateTime
              object. Try this instead:

              MessageBox.Show (Convert.ToDate Time("12/15/06").ToString(" yyMMdd"))
              >
              Pay attention with dates as "1/5/2006" where you can't tell if 1 is the day
              or the month.

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Convert a date string

                fpvt,

                Or in true VisualBasic
                CDate("12/15/06").ToString(" yyMMdd")

                Cor

                <fpvt2@yahoo.co mschreef in bericht
                news:1166200114 .018476.103570@ j72g2000cwa.goo glegroups.com.. .
                Thank you, eveybody.
                Convert.ToDateT ime("12/15/06").ToString(" yyMMdd")) does it.
                >
                Thanks.
                >
                >
                Fabio Z wrote:
                >"rowe_newsgrou ps" <rowe_email@yah oo.comha scritto nel messaggio
                >>
                AFAIK the .ToString(...da teformat...) only works with a DateTime
                object. Try this instead:
                >
                MessageBox.Show (Convert.ToDate Time("12/15/06").ToString(" yyMMdd"))
                >>
                >Pay attention with dates as "1/5/2006" where you can't tell if 1 is the
                >day
                >or the month.
                >

                Comment

                • Fabio

                  #9
                  Re: Convert a date string


                  "Cor Ligthert [MVP]" <notmyfirstname @planet.nlha scritto nel messaggio
                  news:uEOE2WIIHH A.924@TK2MSFTNG P02.phx.gbl...
                  fpvt,
                  >
                  Or in true VisualBasic
                  CDate("12/15/06").ToString(" yyMMdd")
                  >
                  I repeat: watch out for locale dates.
                  I don't think that the date "12/15/06" is hard coded.
                  I.e. in my country (Italy) that date does not exists, because 15 would be
                  the month (dd/mm/yyyy).



                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: Convert a date string

                    Fabio,

                    Normally I am the one who is writting as you did in the dotNet newsgroup.

                    I was missing the date completely.,

                    :-)

                    Cor

                    "Fabio" <znt.fabio@virg ilio.itschreef in bericht
                    news:OFBpWPVIHH A.1064@TK2MSFTN GP04.phx.gbl...
                    >
                    "Cor Ligthert [MVP]" <notmyfirstname @planet.nlha scritto nel messaggio
                    news:uEOE2WIIHH A.924@TK2MSFTNG P02.phx.gbl...
                    >fpvt,
                    >>
                    >Or in true VisualBasic
                    >CDate("12/15/06").ToString(" yyMMdd")
                    >>
                    >
                    I repeat: watch out for locale dates.
                    I don't think that the date "12/15/06" is hard coded.
                    I.e. in my country (Italy) that date does not exists, because 15 would be
                    the month (dd/mm/yyyy).
                    >
                    >
                    >

                    Comment

                    Working...