Results 1 to 8 of 8

Thread: VB.NET: Format Date......[Resolved]

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    VB.NET: Format Date......[Resolved]

    Say i have these XML tag
    Code:
    <book published="New York">
        <date day="12" month="1" year="2005" />    
    </book>
    How do i exactly format the date since i am getting three different attributes (day, month and year) from the server......

    Say i want the format to be "DD/MM/YYYY"...

    I want the format date to be displayed in the listbox...

    Does anyone have any idea to combined all three attributes and format instead of format directly the date as a whole....

    Thanks
    Last edited by toytoy; Jan 28th, 2005 at 12:06 PM.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: VB.NET: Format Date

    I believe your intention is to format the date your given for display in a listbox, correct?

    Then you would probably want to implement a variation of the date formatting options available.

    VB Code:
    1. Dim day As String = "2"
    2.  Dim month As String = "9"
    3.  Dim year As String = "2002"
    4.  
    5.  Dim tempDt As DateTime = New DateTime(Integer.Parse(year, CultureInfo.InvariantCulture), _
    6.            Integer.Parse(month, CultureInfo.InvariantCulture), Integer.Parse(day, CultureInfo.InvariantCulture))
    7.  MessageBox.Show(String.Format("{0}", tempDt.ToString("dd/MM/yyyy")))

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    Re: VB.NET: Format Date

    where you got the CultureInfo....

    I got error saying it is not declared..

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: VB.NET: Format Date

    What's wrong with simply using Date.Parse?

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: VB.NET: Format Date

    You need to include an imports statement at the top of the class.

    Imports System.Globalization

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: VB.NET: Format Date

    Quote Originally Posted by mendhak
    What's wrong with simply using Date.Parse?
    There are apparently some logic holes in Date.Parse and we've been receiving about a dozen errors now and then on valid dates. We don't know for sure what's causing the errors, but constructing the Date object with integer values solved it. My guess is some users even in the US do not use the default US-EN culture format (immigrants?) on their systems.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: VB.NET: Format Date

    Very interesting, this is making me think about all the places I have used it.

    It just goes to show that everyone should adopt the US date format or be annhilated.

  8. #8

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    Re: VB.NET: Format Date

    I use these few lines to solve it...
    VB Code:
    1. Dim niEuro As New System.Globalization.DateTimeFormatInfo
    2. Dim dd, mm, yyyy, pa As String
    3.  
    4. dd = nod2.Attributes("day").Value
    5. mm = nod2.Attributes("month").Value
    6. yyyy = nod2.Attributes("year").Value
    7. pa = dd & "/" & mm & "/" & yyyy
    8. niEuro.ShortDatePattern = "dd/MM/yyyy"
    9. Dim dataEuro As Date = Date.Parse(pa, niEuro)
    10. MessageBox.Show(dataEuro)

    but nemaroller, your solution is still the best..

    Last edited by toytoy; Jan 28th, 2005 at 01:05 PM.

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