473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

String to date

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
18 2104
"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
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
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
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
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
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
"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
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
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
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
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
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
"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
"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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a...
3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
6
by: Thomas Scheiderich | last post by:
I have the following page as test.aspx: *************************************************** <html> <head> <title>Hello and Welcome Page</title> </head> <body> <center> <% Dim CurrentDate As...
16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
3
by: carl.barrett | last post by:
Hi, I have a number of buttons on a form which run mailmerges. Next to each button is a text box/control that the user enters a date into when the letter was created/merged. When the user...
50
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse...
22
by: Simon Says | last post by:
Dear all, I have the following: dim tmpString as String = "11/21/2004 07:55:33" dim tmpDate as Date = CDate(tmpString) But I keep getting <cast from string "11/21/2004 07:55:33" to type...
2
by: Kun | last post by:
I have an html form that takes dates and inserts them into a mysql file. Currently, users have to type in dates in the yyyy-mm-dd format. As of now, this process works with the sql. However, I...
1
by: jwf | last post by:
This question continues on from a previous post "DATE to string" but I think it deserves a new thread. In my previous post I was trying to convert a DATE to string in a NON MFC C++ application...
5
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.