DateTime在不同的环境中解析不同

本文关键字:DateTime 环境 | 更新日期: 2023-09-27 17:52:11

我正在向SharePoint列表中插入一些数据(通过web服务),并且在我的本地机器上,我设置了一个日期字段,如下所示(在本例中是硬编码)

<Field Name='TimeOnScene'>" + DateTime.Parse("13/12/2011 1:00").ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Field>

,它在我的本地机器上工作得很好,但如果我将它发布到我们的web主机并运行完全相同的代码,我得到

{"Message":"String was not recognized as a valid DateTime.","StackTrace":"
//
//
 System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)'r'n   at System.DateTime.Parse(String s)'r'n   "ExceptionType":"System.FormatException"}

这怎么可能?

提前感谢。

编辑:

我们也在两周前从主机转移到另一个主机,以前从未遇到过这个问题。

DateTime在不同的环境中解析不同

使用DateTime。ParseExact而不是Parse,转换将像下面的代码:

CurDate = DateTime.ParseExact(YourDateString, "dd/MM/yyyy hh:mm", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None)

主机可以在其机器上设置不同的local in。在文档中The string s is parsed using formatting information in the current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture.