Date Format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles A. Lackman

    #1

    Date Format

    Hello,

    I an trying to get away from using a Date Time Picker and would like to know
    how to make a textbox format to a date, i.e.

    6604 would be converted to 06/06/04

    and if the user type in
    6/6/04 it would be converted to 06/06/04

    I have experimented with formating the information with little to no success
    ie.

    textbox1.text = format(textbox2 .text, "0#/0#/0#")

    or even

    Dim ADate as Date
    ADate = CType(TextBox2. text, Date)
    Format(ADate, "mm/dd/yy")
    Textbox1.text = ADate

    Anyway, I have tried alot of different ways to make it work with no success.

    Any suggestions and examples will be greatly appreciated.

    Chuck

    P.S. I have wondered if an input mask would solve this problem, but I an not
    sure how to accomplish this.


  • Sijin Joseph

    #2
    Re: Date Format

    You could use a Masked Edit control to do this, take a look at these
    resources

    The MaskedEditContr ol sample from MSDN
    http://msdn.microsoft.com/vcsharp/do...s/default.aspx

    From CodeProject



    From C#Corner


    From WindowsForms.ne t


    Sijin Joseph



    Charles A. Lackman wrote:[color=blue]
    > Hello,
    >
    > I an trying to get away from using a Date Time Picker and would like to know
    > how to make a textbox format to a date, i.e.
    >
    > 6604 would be converted to 06/06/04
    >
    > and if the user type in
    > 6/6/04 it would be converted to 06/06/04
    >
    > I have experimented with formating the information with little to no success
    > ie.
    >
    > textbox1.text = format(textbox2 .text, "0#/0#/0#")
    >
    > or even
    >
    > Dim ADate as Date
    > ADate = CType(TextBox2. text, Date)
    > Format(ADate, "mm/dd/yy")
    > Textbox1.text = ADate
    >
    > Anyway, I have tried alot of different ways to make it work with no success.
    >
    > Any suggestions and examples will be greatly appreciated.
    >
    > Chuck
    >
    > P.S. I have wondered if an input mask would solve this problem, but I an not
    > sure how to accomplish this.
    >
    >[/color]

    Comment

    • Cor Ligthert

      #3
      Re: Date Format

      Charles,

      Before I give an answer, what does this date mean in your situation?

      11111

      Cor

      "Charles A. Lackman"
      [color=blue]
      > Hello,
      >
      > I an trying to get away from using a Date Time Picker and would like to
      > know how to make a textbox format to a date, i.e.
      >
      > 6604 would be converted to 06/06/04
      >
      > and if the user type in
      > 6/6/04 it would be converted to 06/06/04
      >
      > I have experimented with formating the information with little to no
      > success ie.
      >
      > textbox1.text = format(textbox2 .text, "0#/0#/0#")
      >
      > or even
      >
      > Dim ADate as Date
      > ADate = CType(TextBox2. text, Date)
      > Format(ADate, "mm/dd/yy")
      > Textbox1.text = ADate
      >
      > Anyway, I have tried alot of different ways to make it work with no
      > success.
      >
      > Any suggestions and examples will be greatly appreciated.
      >
      > Chuck
      >
      > P.S. I have wondered if an input mask would solve this problem, but I an
      > not sure how to accomplish this.
      >
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Date Format

        "Charles A. Lackman" <Charles@Create ItSoftware.net> schrieb:[color=blue]
        > I an trying to get away from using a Date Time Picker and would
        > like to know how to make a textbox format to a date, i.e.
        >
        > 6604 would be converted to 06/06/04
        >
        > and if the user type in
        > 6/6/04 it would be converted to 06/06/04
        >
        > I have experimented with formating the information with little to no
        > success ie.
        >
        > textbox1.text = format(textbox2 .text, "0#/0#/0#")[/color]

        This won't work.
        [color=blue]
        > Dim ADate as Date
        > ADate = CType(TextBox2. text, Date)[/color]

        Use 'DateTime.Parse Exact' with a specific format instead.
        [color=blue]
        > Format(ADate, "mm/dd/yy")[/color]

        Notice that "mm" is the pattern for minutes, use "MM" instead.

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

        Comment

        • suzzimmi

          #5
          Re: Date Format

          "Cor Ligthert" wrote:
          [color=blue]
          > Charles,
          >
          > Before I give an answer, what does this date mean in your situation?
          >
          > 11111
          >
          > Cor[/color]

          Upstair may consider a way to manipulate the date format in your desired
          way, like yy/mm/dd or dd/mm/yy or yy/dd/mm or mm/dd/yy or few more
          combinations. If it's in yy/mm/dd, then 11111 must be in 11/11/1 but this is
          not in right format of your desire, so you need to convert it in 11/11/01
          either by using your own defined function call or thr built-in calls under
          Date/ Time category, depending on how you get that 11111.

          Comment

          • suzzimmi

            #6
            RE: Date Format

            > I have experimented with formating the information with little to no success[color=blue]
            > ie.
            >
            > textbox1.text = format(textbox2 .text, "0#/0#/0#")[/color]

            Sure this one is failed. If a user enters 12/12/4, then the above line would
            give out an error msg coz you can't add a 0 in front of 12.

            You may add a length check when a user enters a date. If it's less than 6,
            then the entry is not allowed. In this way, you may have easier life to
            format the string in your own way.
            But you can also do it without previous check. feeding in two digits every
            time and adding a 0 if it's a single digits would work too.

            it's lots of ways to implement that by the way...

            Comment

            • Cor Ligthert

              #7
              Re: Date Format

              Susumi
              [color=blue]
              > Upstair may consider a way to manipulate the date format in your desired
              > way, like yy/mm/dd or dd/mm/yy or yy/dd/mm or mm/dd/yy or few more
              > combinations. If it's in yy/mm/dd, then 11111 must be in 11/11/1 but this
              > is
              > not in right format of your desire, so you need to convert it in 11/11/01
              > either by using your own defined function call or thr built-in calls under
              > Date/ Time category, depending on how you get that 11111.[/color]

              And why it is not?
              01:11:2011

              Or

              11:01:2011

              Cor


              Comment

              Working...