日期和日期问题服务器上的时间格式
我在尝试过滤两个日期之间的列表时遇到问题。我有一个在本地计算机 .NET 3.5 上运行的解决方案。该解决方案运行良好,没有任何错误。但是当我把它放在我们的服务器上(2008)时,我得到了
System.FormatException: String was not recognizes as a valid DateTime。
如果我将计算机的日期/时间格式从英语(英国)更改为英语(美国),我可以在本地重现该问题。
问题是我已将服务器设置设置为与本地解决方案完全相同,但仍然收到错误。
涉及的代码是
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 DateTime.Parse(this.ViewState["EndDate"].ToString());
}
else { return DateTime.Today; }
}
private set
{
this.ViewState["EndDate"] = value.ToString();
}
}
请帮忙!
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 DateTime.Parse(this.ViewState["EndDate"].ToString());
}
else { return DateTime.Today; }
}
private set
{
this.ViewState["EndDate"] = value.ToString();
}
}
Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用解析精确。这将允许您指定日期格式,而不考虑计算机的区域设置。
Stack Overflow 上有很多问题。
.NET 将字符串解析为 DateTime
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,尝试为
DateTime.Parse
方法指定区域性信息,例如:更多信息请参见 http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
Hej, try specifying the culture info to the
DateTime.Parse
method, ex:more here http://msdn.microsoft.com/en-us/library/1k1skd40.aspx