Results 1 to 5 of 5

Thread: [RESOLVED] Date format trouble

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Resolved [RESOLVED] Date format trouble

    I'm writing an application that stores some user data, one of the items is an expiry date entered into a text box in the UK format (dd/mm/yy).

    Before I store this in the Access DB I'm trying to convert it to a date (as it's a string when it's entered).

    If I enter the date 22/10/05 and then call CDATE(txtExpiryDate.Text) I get the following error.

    Cast from string "22/10/04" to type 'Date' is not valid

    I'm assuming it's because it's expecting the US format, the question is how do I convert a string in the UK format to a date I can store in Access ?

    I want it as a date because later in the application I want to compare it against todays date to check whats expired.

    Hope this makes sense.

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Date format trouble

    You may use a DateTimePicker control to avoid having to convert a date that you don't know the format.

    But if you really have to convert a string to a date, you may use the New Date constructor

    Dim MyDate As Date = New Date(2005, 9, 22)

    Or try with the Parse method of a DateTime dataType

    Dim MyDate As Date = Date.Parse(MyDateString)
    Using VS 2010 on Fw4.0

  3. #3
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Date format trouble

    This is a hack but it works:

    VB Code:
    1. Dim myDate as date
    2.  
    3. myDate = SwapDayMonth("20/10/04")
    4.  
    5. Private Function SwapDayMonth(ByVal d as String) as Date
    6.     Dim fSlash, lSlash, diff as Integer
    7.     Dim tMonth, tDay, tYear As String
    8.  
    9.     fSlash = d.IndexOf("/")
    10.     lSlash = d.LastINdexOf("/")
    11.     diff = lSlash - fSlash
    12.  
    13.     tDay = d.Substring(0, fSlash)
    14.     tMonth = d.Substring(lSlash - diff +1, diff-1)
    15.     tyear = d.Substring(lSlash +1,2)
    16.  
    17.     Return Convert.ToDate(tMonth & "/" & tDay & "/" &tyear)
    18. End Function

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Date format trouble

    Thanks for the help, I'm getting the following error with your code Campster

    'ToDate' is not a member of 'System.Convert'

    any ideas ?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [RESOLVED] Date format trouble

    I've worked around it by passing a converted string back and storing it in the database, now when i compare against todays date it works fine.
    Thanks again for your 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