Results 1 to 8 of 8

Thread: [Resolved] Help with converting date!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Location
    Ohio
    Posts
    153

    Resolved [Resolved] Help with converting date!

    I am trying create code that prompts a user for the date in "mm/dd/yy" form then will display in a messagebox as "Month day, year".

    Example: 05/11/06 - will display - May 11, 2006

    I tryed using select case for it, but that didn't work to well for me, and is probably longer then another way out there. Could somebody please help?

    Thanks.
    Last edited by hlstriker; May 11th, 2006 at 08:03 AM. Reason: Resolved

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with converting date!

    VB Code:
    1. Dim dateString As String = "05/11/06"
    2.         Dim myDate As Date = Date.ParseExact(dateString, "dd/MM/yy", Nothing)
    3.  
    4.         MessageBox.Show(myDate.ToString("MMM dd, yyyy"))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    Re: Help with converting date!

    If the user is prompted for a date using a DateTimePicker control then it is really easy.

    The reuslt of the DateTimePicker will be it DateTimePicker.Value - which is a Date type,and as succh can be redisplayed using a ToString

    ie, dtpInputDate.Value.Tostring("mmm dd, yyyy")

    If you are using a textbox or similar to enter the date (and why - the datetimepicker is so much more user friendly) then your best bet is to convert the textstring to a date type.

    I am pretty sure that

    VB Code:
    1. dim objDate as Date
    2. objDate = ctype(txtInput.Text, Date)

    Will not work in format dd/mm/yy - and besides if the user makes a mistake typing it in, the cast will cause an exception to occur.

    Is there any reason you dont want to use a datetimepicker?

  4. #4
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    Re: Help with converting date!

    or you can just do what jmcilhinny suggests

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Location
    Ohio
    Posts
    153

    Re: Help with converting date!

    Is this allowing input of any date? It seems this is just getting the date from the computer. I was trying to input any date in "mm/dd/yy" form into a textbox, then it would convert it to the "Month day, year" form displaying it in a messagebox.

    I probably should have used more examples in my first post. Sorry @_@

    Examples:
    06/18/02 - will display - June 18, 2002
    05/11/06 - will display - May 11, 2006

  6. #6
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    Re: Help with converting date!

    substitute your txtbox.text for jmc's datestring

    VB Code:
    1. Dim myDate As Date = Date.ParseExact(txtbox.Text, "dd/MM/yy", Nothing)
    2. MessageBox.Show(myDate.ToString("MMM dd, yyyy"))

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Location
    Ohio
    Posts
    153

    Re: Help with converting date!

    Ok, thanks alot everyone !

  8. #8
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    Re: [Resolved] Help with converting date!

    You may want to stick a try/catch block around it though otherwise it will all go titsup if the user types in garbage.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width