By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

String to date

P: n/a
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)
Nov 21 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"David Gacek" <so*****@hotmail.com> wrote in news:uTyL3#F0EHA.3468
@TK2MSFTNGP14.phx.gbl:
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)

Try TryToConvert = CDate("11/22/2004") That should work.

http://msdn.microsoft.com/library/de...l=/library/en-
us/vblr7/html/vagrptypeconversion.asp
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
How about DateTime.ParseExact() ?

"David Gacek" <so*****@hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP14.phx.gbl...
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)

Nov 21 '05 #4

P: n/a
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"))

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"David Gacek" <so*****@hotmail.com> wrote in news:uTyL3#F0EHA.3468
@TK2MSFTNGP14.phx.gbl:
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)

Try TryToConvert = CDate("11/22/2004") That should work.

http://msdn.microsoft.com/library/de...l=/library/en-
us/vblr7/html/vagrptypeconversion.asp
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #5

P: n/a
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

"Shiva" <sh******@online.excite.com>
How about DateTime.ParseExact() ?

"David Gacek" <so*****@hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP14.phx.gbl...
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)

Nov 21 '05 #6

P: n/a
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 ?



"Cor Ligthert" <no************@planet.nl> wrote in message
news:O6**************@tk2msftngp13.phx.gbl...
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

"Shiva" <sh******@online.excite.com>
How about DateTime.ParseExact() ?

"David Gacek" <so*****@hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP14.phx.gbl...
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)


Nov 21 '05 #7

P: n/a
"David Gacek" <so*****@hotmail.com> wrote in
news:uV**************@TK2MSFTNGP11.phx.gbl:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #8

P: n/a
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.

"David Gacek" <so*****@hotmail.com> wrote in message
news:e1**************@TK2MSFTNGP09.phx.gbl...
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 ?



"Cor Ligthert" <no************@planet.nl> wrote in message
news:O6**************@tk2msftngp13.phx.gbl...
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

"Shiva" <sh******@online.excite.com>
How about DateTime.ParseExact() ?

"David Gacek" <so*****@hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP14.phx.gbl...
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)



Nov 21 '05 #9

P: n/a
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
Nov 21 '05 #10

P: n/a
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.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:O6**************@tk2msftngp13.phx.gbl...
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

"Shiva" <sh******@online.excite.com>
How about DateTime.ParseExact() ?

"David Gacek" <so*****@hotmail.com> wrote in message
news:uT**************@TK2MSFTNGP14.phx.gbl...
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)


Nov 21 '05 #11

P: n/a
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
Nov 21 '05 #12

P: n/a
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)

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"David Gacek" <so*****@hotmail.com> wrote in
news:uV**************@TK2MSFTNGP11.phx.gbl:
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.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 21 '05 #13

P: n/a
"David Gacek" <so*****@hotmail.com> schrieb:
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")
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #14

P: n/a
"David Gacek" <so*****@hotmail.com> wrote in
news:ui**************@tk2msftngp13.phx.gbl:
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?

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #15

P: n/a
"Shiva" <sh******@online.excite.com> schrieb:
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())
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #16

P: n/a
Thanks for the replys
I think i've worked it out

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
"Shiva" <sh******@online.excite.com> schrieb:
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())
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #17

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

"Herfried K. Wagner [MVP]" wrote:
"Shiva" <sh******@online.excite.com> schrieb:
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())
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #18

P: n/a
Ann
I got past the System portion. Now I have an error on the Globalization.

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

"Herfried K. Wagner [MVP]" wrote:
"Shiva" <sh******@online.excite.com> schrieb:
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())
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.