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

Convert.ToDateTime ERROR

P: n/a
I am running an ASP.net program written in VB. At one point I try to convert
a date string into a date time object... this string is from a central dev
server and the code works on many other machines.

Convert.ToDateTime("")

I get the string in the format MM/DD/YYYY (06/26/2007)

The code breaks and I get the "String was not recognized as a valid
DateTime" error message unless I manually change the input string to
DD/MM/YYY (26/06/2007).

I went into control panel "Regional Settings" and changed from Canadian to
US... where short date is MM/DD/YYYY... that didn't seem to help.

Does anyone know how I can fix this rather annoying issue?
Dec 10 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"andrew" <an****@discussions.microsoft.comschrieb
I am running an ASP.net program written in VB. At one point I try
to convert a date string into a date time object... this string is
from a central dev server and the code works on many other machines.

Convert.ToDateTime("")

I get the string in the format MM/DD/YYYY (06/26/2007)

The code breaks and I get the "String was not recognized as a valid
DateTime" error message unless I manually change the input string to
DD/MM/YYY (26/06/2007).

I went into control panel "Regional Settings" and changed from
Canadian to US... where short date is MM/DD/YYYY... that didn't seem
to help.

Does anyone know how I can fix this rather annoying issue?
Dim d As Date = Date.ParseExact("06/26/2007", "MM\/dd\/yyyy", Nothing)
Armin
Dec 10 '07 #2

P: n/a
I'm working in a group of maybe 50 developers... and the code works on all of
their machines... in fact before my machine was re-ghosted it also worked on
mine. I don't think changing the code is an option for me.

"Armin Zingler" wrote:
"andrew" <an****@discussions.microsoft.comschrieb
I am running an ASP.net program written in VB. At one point I try
to convert a date string into a date time object... this string is
from a central dev server and the code works on many other machines.

Convert.ToDateTime("")

I get the string in the format MM/DD/YYYY (06/26/2007)

The code breaks and I get the "String was not recognized as a valid
DateTime" error message unless I manually change the input string to
DD/MM/YYY (26/06/2007).

I went into control panel "Regional Settings" and changed from
Canadian to US... where short date is MM/DD/YYYY... that didn't seem
to help.

Does anyone know how I can fix this rather annoying issue?

Dim d As Date = Date.ParseExact("06/26/2007", "MM\/dd\/yyyy", Nothing)
Armin
Dec 10 '07 #3

P: n/a
"andrew" <an****@discussions.microsoft.comschrieb
I'm working in a group of maybe 50 developers... and the code works
on all of their machines... in fact before my machine was re-ghosted
it also worked on mine. I don't think changing the code is an
option for me.

"Armin Zingler" wrote:
"andrew" <an****@discussions.microsoft.comschrieb
I am running an ASP.net program written in VB. At one point I
try to convert a date string into a date time object... this
string is from a central dev server and the code works on many
other machines.
>
Convert.ToDateTime("")
>
I get the string in the format MM/DD/YYYY (06/26/2007)
>
The code breaks and I get the "String was not recognized as a
valid DateTime" error message unless I manually change the input
string to DD/MM/YYY (26/06/2007).
>
I went into control panel "Regional Settings" and changed from
Canadian to US... where short date is MM/DD/YYYY... that didn't
seem to help.
>
Does anyone know how I can fix this rather annoying issue?
Dim d As Date = Date.ParseExact("06/26/2007", "MM\/dd\/yyyy",
Nothing)

Convert.todatetime uses the current culture. The string to be parsed is not
guaranteed to be in the current culture's datetime format. That's why the
code is not correct and should be changed.

I don't know why it still doesn't work after changing the regional settings.
Have you also tried it in a new test application on the same machine in a
user account?
Armin

Dec 10 '07 #4

P: n/a
I managed to work around it by changing my culture in my machine.config file.

"Armin Zingler" wrote:
"andrew" <an****@discussions.microsoft.comschrieb
I'm working in a group of maybe 50 developers... and the code works
on all of their machines... in fact before my machine was re-ghosted
it also worked on mine. I don't think changing the code is an
option for me.

"Armin Zingler" wrote:
"andrew" <an****@discussions.microsoft.comschrieb
I am running an ASP.net program written in VB. At one point I
try to convert a date string into a date time object... this
string is from a central dev server and the code works on many
other machines.

Convert.ToDateTime("")

I get the string in the format MM/DD/YYYY (06/26/2007)

The code breaks and I get the "String was not recognized as a
valid DateTime" error message unless I manually change the input
string to DD/MM/YYY (26/06/2007).

I went into control panel "Regional Settings" and changed from
Canadian to US... where short date is MM/DD/YYYY... that didn't
seem to help.

Does anyone know how I can fix this rather annoying issue?
>
Dim d As Date = Date.ParseExact("06/26/2007", "MM\/dd\/yyyy",
Nothing)


Convert.todatetime uses the current culture. The string to be parsed is not
guaranteed to be in the current culture's datetime format. That's why the
code is not correct and should be changed.

I don't know why it still doesn't work after changing the regional settings.
Have you also tried it in a new test application on the same machine in a
user account?
Armin

Dec 10 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.