0

我知道有很多关于这个问题的帖子,但没有一个对我有用,因此我想自己发一个。我希望我不是垃圾邮件。

我要做的就是将字符串转换为 DateTime 对象。它在 VS Web 开发服务器上运行良好,但是当我将它发布到 IIS 时,它开始抛出异常“字符串未被识别为有效的 DateTime”。我浏览了许多帖子并尝试了以下代码,但都失败了。为简单起见,我硬编码了 DateTime 字符串。请注意,字符串中 DateTime 的格式是固定的,我只能使用它。

Convert.ToDateTime("27-6-2012 9:05 PM")

// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below.
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US
Convert.ToDateTime("27-6-2012 9:05 PM", CultureInfo.CurrentCulture)

DateTime.Parse("27-6-2012 9:05 PM")

// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below.
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US
DateTime.Parse("27-6-2012 9:05 PM", CultureInfo.CurrentCulture)

// Just for the sake of it I also used CurrentUICulture and InstalledUICulture in the line below.
// Note: For me CurrentCulture = CurrentUICulture = InstalledUICulture = en-US
DateTime.ParseExact("27-6-2012 9:05 PM", "{0:d-M-yyyy h:mm tt}", CultureInfo.CurrentCulture)

然后只是为了实验,我将字符串中日期的格式更改为“6/27/2012 9:05 PM”。我发现除了 DateTime.ParseExact 之外,上述所有方法都有效。对我来说,如果上述任何一个代码有效,我很好,但问题是我无法更改字符串中日期的格式,这是我无法控制的参数。

这看起来像一个微不足道的问题,但我不知道我错过了什么?这太尴尬了。任何人都可以帮助我吗?

提前致谢。

4

1 回答 1

0

服务器上的区域设置可能与您的开发环境中的不同。尝试这个。http://support.microsoft.com/kb/241671

编辑 - 查看区域设置下的控制面板。这将影响服务器读取日期的方式。

在此处输入图像描述

于 2012-06-28T17:33:39.190 回答