Marco Trapanese <marcotrapaneseNOSPAM@libero.it> wrote in news:v3spi.74444
$U01.683421@twister1.libero.it:
> CDate("#24/04/2007#") doesn't?
>
> It says: index out of bounds. Index of what matrix?
> I have a string (actually a textbox) and I want to convert it to a
> DateTime value. How?
Try CDate("24/04/2007")
What does that return?
Marco Trapanese - 24 Jul 2007 20:51 GMT
> Try CDate("24/04/2007")
It returns the same error.
Bye
Marco / iw2nzm
"Marco Trapanese" <marcotrapaneseNOSPAM@libero.it> schrieb:
> CDate(#24/04/2007#) works but,
... but doesn't make sense because '#24/04/2007#' is a VB 'Date' (=
'DateTime') literal.
> CDate("#24/04/2007#") doesn't?
Read the documentation on 'CDate' to determine which formats it is able to
interpret correctly.
> I have a string (actually a textbox) and I want to convert it to a
> DateTime value. How?
I suggest to take a look at 'Date.Parse' and 'Date.ParseExact' (documented
for the 'DateTime' data type). In addition you may want to use a
datetimepicker control instead of a textbox control for entering dates.

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Marco Trapanese - 25 Jul 2007 06:30 GMT
> Read the documentation on 'CDate' to determine which formats it is able
> to interpret correctly.
I did that. I'll do again.
> I suggest to take a look at 'Date.Parse' and 'Date.ParseExact'
> (documented for the 'DateTime' data type). In addition you may want to
> use a datetimepicker control instead of a textbox control for entering
> dates.
Thanks for the suggestions. I can't use a datatimepicker because the
string shown into the textbox comes from a serial connection.
Bye
Marco / iw2nzm
Martin H. - 25 Jul 2007 11:06 GMT
Hello Marco,
> Thanks for the suggestions. I can't use a datatimepicker because the
> string shown into the textbox comes from a serial connection.
So it is a fixed format (not related to locale settings)?
In This Case...
Dim SerialDateString As String = "#24/04/2007#"
Dim MyDate As New Date(Strings.Mid(SerialDateString, 8, 4) _
, Strings.Mid(SerialDateString, 5, 2) _
, Strings.Mid(SerialDateString, 2, 2))
MsgBox(MyDate.ToString)
But one thing is strange. I tried to convert SerialDateString to
Date and it worked without any problems...
Dim tDate As Date = CDate(SerialDateString)
However, the above solution has the advantage that, no matter of which
order the date on your computer is (DD.MM.YYYY, MM.DD.YYYY, YYYY.MM.DD),
it will always work.
Best regards,
Martin
Kevin S Gallagher - 25 Jul 2007 17:08 GMT
Why not were the MaskedTextBox would be no different then the string.mid...
then take the extracted elements and convert to a date or simply combine the
date elements as you are already doing and do the second example
Example 1 I used a MaskedTextBox which could have easily been what you did
in your example below, doesn't matter how the elements are gotten.
Dim Value() As String = MaskedTextBox1.Text.Split("/"c)
Dim MyDate As New DateTime(CInt(Value(2)), CInt(Value(0)),
CInt(Value(1)))
Again MaskedTextBox...I like TryParse and TryParseExact.
Dim MyDate As DateTime
If DateTime.TryParse(MaskedTextBox1.Text, MyDate) Then
Console.WriteLine(MyDate.ToShortDateString)
End If
Both worked fine for me in VS2005
> Hello Marco,
>
[quoted text clipped - 22 lines]
>
> Martin