Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / July 2007

Tip: Looking for answers? Try searching our database.

Convert a string to a date

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marco Trapanese - 24 Jul 2007 20:04 GMT
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
Spam Catcher - 24 Jul 2007 20:29 GMT
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
Herfried K. Wagner [MVP] - 24 Jul 2007 21:43 GMT
"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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.