String to date

D

David Gacek

I've tried several different ways all with the same reults. Invalid date

Dim ddate As String

ddate = "11/22/2004"

Dim TryToConvert As Date = Date.Parse(ddate)

Dim TryToConvert2 As Date = Convert.ToDateTime(ddate)

MsgBox(CDate(ddate) & TryToConvert & " " & TryToConvert2)
 
C

Cor Ligthert

Lucas,

For people who are Googling this newsgroup and find your sample,
Try TryToConvert = CDate("11/22/2004") That should work
Try TryToConvert = CDate("11/22/2004") That should work in the USA

A better sample can be
\\\
Dim mydate As String = New Date(2004, 11, 22).ToString
Dim TryToConvert As Date = CDate(mydate)
///

The last just an idea,

Cor
 
S

Shiva

How about DateTime.ParseExact() ?

I've tried several different ways all with the same reults. Invalid date

Dim ddate As String

ddate = "11/22/2004"

Dim TryToConvert As Date = Date.Parse(ddate)

Dim TryToConvert2 As Date = Convert.ToDateTime(ddate)

MsgBox(CDate(ddate) & TryToConvert & " " & TryToConvert2)
 
D

David Gacek

Can you please provide me with a working example
as you can see this code didn't work either
Dim ddate As String

ddate = "11/22/2004"

Dim TryToConvert As Date

MsgBox(TryToConvert = CDate("11/22/2004"))
 
C

Cor Ligthert

Shiva,
How about DateTime.ParseExact() ?

That you can use with your sample:

When it is a field from a textbox, than you have to be absolute sure that
your program only is used in a USA datetime format zone, by instance for
Canada that is not sure.

When it is in a document or webpage when you are 100% sure that it is
returned from the zone above.

I hope this helps?

Cor
 
D

David Gacek

DateTime.ParseExac can i use that in any time zone ? and how can i use it to
return the date in mmddyyyy format Month/Day/Year ?
 
L

Lucas Tam

Can you please provide me with a working example
as you can see this code didn't work either
Dim ddate As String

ddate = "11/22/2004"

Dim TryToConvert As Date

MsgBox(TryToConvert = CDate("11/22/2004"))


Works A-OK on my side...


Dim DateString As String = "11/22/2004"
Dim TestDate As Date = CDate(DateString)
MsgBox(TestDate)

I don't know what's wrong with your development environment. What error
did you get?

Are you in the UK? Possibly CDate is trying to convert your US Date into
a UK date - which would make it an invalid date.
 
S

Shiva

ParseExact() basically lets you specify the date/time format to expect when
parsing the string value.

For example, DateTime.ParseExact ("2004/11/22", "yyyy/MM/dd", null) will get
you a DateTime value with correct date value interpretations. And
DateTime.ParseExact ("2004/11/22", "yyyy/MM/dd", null).ToString
("MM/dd/yyyy") returns the value in the specified format - "11/22/2004".

Hope this is what you are looking for.

DateTime.ParseExac can i use that in any time zone ? and how can i use it to
return the date in mmddyyyy format Month/Day/Year ?
 
C

Cor Ligthert

David,

The datetime in Microsoft OS has an internal format in the USA style and not
confirm the ISO style (as I wished it was, because that USA style is very
confusing outside that culture area).

http://www.w3.org/TR/NOTE-datetime

So with string to datetime will the datetime forever in the same way.

I hope this helps?

Cor
 
S

Shiva

Cor,

DateTime.ParseExact() lets you specifiy the format that the date/time string
value is exepcted to be in. If you, for example, have the string as
2004/10/20, you can tell DateTime to interpret it as "yyyy/MM/dd" and you
get the correct day, month and year values.

Of course, an exception is raised if the specified string value is not as
per the given format.

Shiva,
How about DateTime.ParseExact() ?

That you can use with your sample:

When it is a field from a textbox, than you have to be absolute sure that
your program only is used in a USA datetime format zone, by instance for
Canada that is not sure.

When it is in a document or webpage when you are 100% sure that it is
returned from the zone above.

I hope this helps?

Cor
 
C

Cor Ligthert

Shiva,

There are long discussions about this in this newsgroup between me and
(Herfried and Jay),
Of course, an exception is raised if the specified string value is not as
per the given format.
Only when the day is above 12 (with leapmonths 13).

Cor
 
D

David Gacek

An unhandled exception of type 'System.InvalidCastException' occurred in
microsoft.visualbasic.dll

Additional information: Cast from string "11/22/2004" to type 'Date' is not
valid.
Dim DateString As String = "11/22/2004"

'' On the next line i get the error

Dim TestDate As Date = CDate(DateString)

MsgBox(TestDate)
 
H

Herfried K. Wagner [MVP]

David Gacek said:
DateTime.ParseExac can i use that in any time zone ? and how can i use it
to
return the date in mmddyyyy format Month/Day/Year ?

'DateTime.Parse' is used for the conversion in the 'String' -> 'DateTime'
direction.

Use 'DateTime.ToString' to convert a 'DateTime' to a string. This method is
overloaded and allows you to specify a format:

\\\
Dim s As String = d.ToString("MMddyyyy")
///
 
L

Lucas Tam

An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "11/22/2004" to type 'Date'
is not valid.
Dim DateString As String = "11/22/2004"

'' On the next line i get the error

Dim TestDate As Date = CDate(DateString)

MsgBox(TestDate)

Yup, here is my EXACT code:

Dim DateString As String = "11/22/2004"
Dim TestDate As Date = CDate(DateString)
MsgBox(TestDate = CDate(DateString))

In your Regional Settings (Windows Control Panel), what is your date
settings set as?
 
H

Herfried K. Wagner [MVP]

Shiva said:
I've tried several different ways all with the same reults. Invalid date

Dim ddate As String

ddate = "11/22/2004"

Dim TryToConvert As Date = Date.Parse(ddate)

If 'ddate' is in a fixed format, I prefer:

\\\
Imports System.Globalization
..
..
..
Dim ddate As String = "11/22/2004"
Dim d As Date = _
Date.ParseExact(ddate, "MM\/dd\/yyyy", CultureInfo.InvariantCulture)
MsgBox(d.ToString())
///
 
D

David Gacek

Thanks for the replys
I think i've worked it out

Herfried K. Wagner said:
If 'ddate' is in a fixed format, I prefer:

\\\
Imports System.Globalization
.
.
.
Dim ddate As String = "11/22/2004"
Dim d As Date = _
Date.ParseExact(ddate, "MM\/dd\/yyyy", CultureInfo.InvariantCulture)
MsgBox(d.ToString())
///
 
G

Guest

Herfried,
Is there a reference I need to include for VB6?
I get a compile error - Invalid qualifer on "System".
Thanks,
Ann
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top