Results 1 to 5 of 5

Thread: newB: can't format date UK to US [working]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    CA
    Posts
    16

    Question newB: can't format date UK to US [working]

    I am still new to VB and .NET
    I have searched this forum and tried lots of code and nothing works. Here is what I want to do and what I have:

    user enters date into textbox1.Text as 28/4/2003
    my code should format it to 4/28/2003 and test if isDate

    'Check if it is a valid date.
    Dim dtDate As Date = textbox1.Text
    Dim strDue As String
    strDue = Format(dtDate, "MM/dd/yyyy") 'this should format

    If IsDate(strDue) = False Then
    lblMessage.Text = "Please enter valid due date."
    lblMessage.Visible() = True
    txtDueDate.Text = ""
    Label1.Text = strDue
    Label1.Visible = True
    Exit Sub
    End If

    The code above returns an error:
    Cast from string "28/4/2003" to type 'Date' is not valid.

    If I make dtDate As String then
    strDue = Format(dtDate, "MM/dd/yyyy")
    returns MM/dd/yyyy in Label1.Text
    I used the Label1 to see what the return value of Format is.

    Please help me understand what is happening!
    Last edited by subegt; Apr 25th, 2003 at 12:59 PM.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    ms-help://MS.VSCC/MS.MSDNVS/vblr7/html/vadatDate.htm

    "Date values must be enclosed within number signs (#) and be in the format m/d/yyyy, for example #5/31/1993#. If you convert a Date value to the String type, the date is rendered according to the short date format recognized by your computer, and the time is rendered according to the time format (either 12-hour or 24-hour) in effect on your computer."

    You may also want to look at DateTime.Parse:

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDateTimeClassParseTopic.htm

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    CA
    Posts
    16
    Thanks SL the DateTime.parse works to swap dd/MM to MM/dd.
    However, why does a wrong date like #$/4/2003 get converted to 4/1/2003 after the parse? I put a Try and Catch in but it does not catch all non number chacters as shown above. Any clue?
    Here is my code:

    Dim strMyDateTimeUK As String
    Dim myDateTimeUS As Date
    strMyDateTimeUK = txtDateUS.Text 'from textbox
    Try
    myDateTimeUS = System.DateTime.Parse( _
    strMyDateTimeUK, _
    dtFormat, _
    System.Globalization. _
    DateTimeStyles.AllowWhiteSpaces)
    Catch ThisExcep As System.ArgumentException
    ' At least one argument has an invalid value.
    lblMessage.Text = "ArgumentException."
    Exit Sub
    Catch ThisExcep As ArgumentOutOfRangeException
    ' The result is later than December 31, 9999.
    lblMessage.Text = "ArgumentOutOfRange."
    Exit Sub
    Catch ThisExcep As InvalidCastException
    ' GivenDate cannot be interpreted As a date/time.
    lblMessage.Text = "InvalidCastException."
    Exit Sub
    Catch ThisExcep As Exception
    ' An unforeseen exception has occurred.
    lblMessage.Text = "Exception."
    Exit Sub
    Finally
    lblMessage.Visible() = True
    End Try

  4. #4
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I dunno, I see exactly what you mean - no exception is generated DateTime.Parse for a great many non-numeric characters (~, !, pretty much any non-alpha I tried). You might also look at ParseExact but ParseExact seems extremely picky (as the name would suggest).

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    CA
    Posts
    16

    Thumbs up

    For now this seems to meet my needs.

    myDateTimeUS = System.DateTime.ParseExact( _
    strMyDateTimeUK, _
    "d", _
    dtFormat, _
    System.Globalization. _
    DateTimeStyles.AllowWhiteSpaces)

    'FYI dtFormat is created using:
    'Dim dtFormat As New System.Globalization.CultureInfo("en-GB", True)

    Thanks for the help!

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