字符串未被识别为有效的日期时间.现有的解决方案对我不起作用



我整个上午都在谷歌上搜索这个问题,但无法解决。

事实上,我下面尝试的解决方案是从这个网站提取的:

 If e.Row.RowType = DataControlRowType.DataRow Then
 Dim eDate As TextBox = DirectCast(e.Row.FindControl("txtEventDate"), TextBox)
 eDate.Text = Request.QueryString("evdates")
 DateTime.ParseExact(eDate.Text, "MM/dd/yyyy", CultureInfo.CurrentCulture)
End If

日期值显示为14/04/2014 00:00:00,但我们希望显示为MM/dd/yyyy。

但是我们一直得到String未被识别为有效的DateTime

我甚至尝试在标记上这样做,但无济于事。

<asp:TextBox ID="txtEventDate" DataFormatString="{0:d}" runat="server"></asp:TextBox>

有什么办法解决这个问题吗?

从你的问题来看,日期的格式是dd/MM/yyyy。英国的风格。此外,您可能还需要使用CultureInfo.InvariantCulture。我不确定您的IIS服务器托管在哪里,或者在机器上配置了什么区域性。

你可以试试:

// read the data from the request
string temp = Request.QueryString("evdates");
// convert the string to datetime object
DateTime d = DateTime.ParseExact(temp, "MM/dd/yyyy", CultureInfo.InvariantCulture);
// format any way you like
eDate.Text = d.ToString("MM/dd/yyyy);

相关内容

最新更新