Conversion from string “31/03/2012” to type 'D

2019-02-27 08:03发布

问题:

My web app is running perfectly in asp vb.net editor. But when i run my web app through IIS7 then i get this error. What am i missing in configuring IIS7? Is there anyone who can suggest something?

Thanks in Advance

回答1:

Because your IIS7 is configured for the English Language and that date is probably Italian or something similar. You'll have to tell to the Date.Parse which culture to use.

Something like

dateValue = Date.Parse(yourDate, CultureInfo.CreateSpecificCulture("it-IT"))

Or you can change the culture in your IIS7

Here there are the instructions

for example if you use the UI

Using the UI Open IIS Manager and navigate to the level you want to manage. (omissis)

In Features View, double-click .NET Globalization.

On the .NET Globalization page, in the property sheet, click to select the global setting you want to edit, and select a value from the drop-down list.

In the Actions pane, click Apply.

Or you could set the culture of your app in the web.config

<system.web>
    <globalization culture="it-IT" uiCulture="it-IT"/>
</system.web>


回答2:

If you are sure that the date is always in exactly that format, then you can use ParseExact instead:

var date = DateTime.ParseExact(
               "31/03/2012",
               "dd/MM/yyyy",
               System.Globalization.CultureInfo.InvariantCulture);


回答3:

You can also use the CDate function to parse the date.

Dim dDate As Date = CDate("31/03/2012")

The advantage of using this function over the DateTime parsing functions is that you can feed it any acceptable format of date string and it will convert it. It will throw an error if it can't parse the date.