转换.c#中的ToDateTime在不同的环境中表现不同



我在c#中有以下代码行:

DateTime dtReportDate = Convert.ToDateTime(_ReportDate);

_ReportDate是一个字符串变量,其值为:21/05/2013 (dd/MM/yyyy)。因此,我尝试将该日期转换为DateTime变量,并执行以下操作:

_ReportDate = string.Format("{0:yyyy/MM/dd}", dtReportDate) + " " + _ReportHour;

如您所见,我需要以yyyy/MM/dd HH: MM的格式连接日期和小时。

当在本地运行这些代码行时,它工作正常。但是,当我把它放在Dev服务器上时,它会抛出以下错误:String未被识别为有效的DateTime

所以,我想问几个问题。此错误是否与服务器的任何配置有关?为什么要转换。ToDateTime在本地可以工作,但在服务器上不行?

任何线索都可以

谢谢

我已经知道如何解决这个问题执行以下代码行:

//Attempts to Parse the Date using the format specified (the third parameter can also be null)
DateTime dtReportDate = DateTime.ParseExact(_ReportDate,"dd/MM/yyyy", CultureInfo.InvariantCulture);

希望对别人有所帮助

最新更新