I'm having a problem trying to filter a list between 2 dates. I have a solution running on our local machine .NET 3.5. The solution runs fine without any errors. But when I put it on our server (2008) I get
System.FormatException: String was not recognized as a valid DateTime.
I can recreate the problem locally if I change my machines date/time format from English (United Kingdom) to English (United states).
The problem is I've set the server setting to exactly the same as the local solution and I am still getting the errors.
The code involved is
public DateTime StartDate
{
get
{
if (this.ViewState["StartDate"] != null)
{
return DateTime.Parse(this.ViewState["StartDate"].ToString());
}
else { return DateTime.Today.AddYears(-1); }
}
private set
{
this.ViewState["StartDate"] = value.ToString();
}
}
public DateTime EndDate
{
get
{
if (this.ViewState["EndDate"] != null)
{
return Dat开发者_如何学运维eTime.Parse(this.ViewState["EndDate"].ToString());
}
else { return DateTime.Today; }
}
private set
{
this.ViewState["EndDate"] = value.ToString();
}
}
Please help!
Use ParseExact. This will allow you to specify a date format, irrespective of the machine's locale.
Lots of questions on Stack Overflow.
.NET Parsing string into DateTime
Hej, try specifying the culture info to the DateTime.Parse
method, ex:
DateTime.Parse(dateString, new CultureInfo("en-US", false))
more here http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
精彩评论