Conversion from string “31/03/2012” to type 'Date' is not valid

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 03:03:22

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>

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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!