|
-
Sep 23rd, 2005, 11:01 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Sep 23rd, 2005, 11:09 AM
#2
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)
-
Sep 23rd, 2005, 11:58 AM
#3
Hyperactive Member
Re: Date format trouble
This is a hack but it works:
VB Code:
Dim myDate as date
myDate = SwapDayMonth("20/10/04")
Private Function SwapDayMonth(ByVal d as String) as Date
Dim fSlash, lSlash, diff as Integer
Dim tMonth, tDay, tYear As String
fSlash = d.IndexOf("/")
lSlash = d.LastINdexOf("/")
diff = lSlash - fSlash
tDay = d.Substring(0, fSlash)
tMonth = d.Substring(lSlash - diff +1, diff-1)
tyear = d.Substring(lSlash +1,2)
Return Convert.ToDate(tMonth & "/" & tDay & "/" &tyear)
End Function
-
Sep 24th, 2005, 07:22 AM
#4
Thread Starter
Hyperactive Member
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 ?
-
Sep 24th, 2005, 07:52 AM
#5
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|