Convert a string to a date

  • Thread starter Thread starter Marco Trapanese
  • Start date Start date
M

Marco Trapanese

Hi!

Why

CDate(#24/04/2007#) works but,

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?

Thanks
Marco / iw2nzm
 
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 said:
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.
 
Herfried said:
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
 
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
 
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
 
Back
Top