Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET C中日期和字符串之间的转换#_C#_Asp.net_String_Datetime - Fatal编程技术网

C# ASP.NET C中日期和字符串之间的转换#

C# ASP.NET C中日期和字符串之间的转换#,c#,asp.net,string,datetime,C#,Asp.net,String,Datetime,我现在在一个动态ASP.NET C#web应用程序中工作,该应用程序使用ODBC连接到Sybase数据库,我的主要问题是应用程序中datetime和string数据类型之间的转换 数据库以以下形式保存日期时间数据:- 7/11/2011 05:05:05 I think it is in the pattern of "MM/dd/yyyy hh:mm:ss" 我需要调用web服务并传递一些datetime参数以获得正确的输出。 web服务接受的静态日期时间格式为:- 11/07/11 09:

我现在在一个动态ASP.NET C#web应用程序中工作,该应用程序使用ODBC连接到Sybase数据库,我的主要问题是应用程序中datetime和string数据类型之间的转换

数据库以以下形式保存日期时间数据:-

7/11/2011 05:05:05
I think it is in the pattern of "MM/dd/yyyy hh:mm:ss"
我需要调用web服务并传递一些datetime参数以获得正确的输出。 web服务接受的静态日期时间格式为:-

11/07/11 09:09:09
I think it is in the pattern of "dd/MM/yy hh:mm:ss"
我尝试分别使用以下函数

string s = d.toString(string format)
DateTime d = Convert.ToDateTime(string s)
DateTime d = s.Datetime.Parse(string format)
DateTime d = s.Datetime.ParseExact(string format)
将我从数据库获得的日期转换为适合传递给web服务的日期,但它总是给我不同的例外情况,如:-

异常:System.FormatException:字符串未被识别为有效的日期时间。位于WebApplication1.WebForm1.callWebservice()的System.DateTime.Parse(字符串s,DateTimeFormatInfo dtfi,datetimestyle样式)处的System.DateTimeParse.Parse(字符串s)

我不知道应该如何正确地使用和转换日期,当我尝试许多示例和函数时,我感到失望,但我无法获得正确的方法


我希望我能找到一个可以提前帮助和感谢的人….

您的Web服务需要字符串还是实际的datetime对象


您是否尝试过以
yyyy-MM-dd hh:MM:ss格式传递日期时间,因为这种格式应该得到普遍认可?

您应该能够使用
DateTime.ParseExact


尝试以下
DateTime d=DateTime.ParseExact(“11/07/11 09:09:09”,“dd/MM/yy hh:MM:ss”,null)

按照链接,这里有关于日期时间格式的所有内容:


谢谢你的回答。我发现我的计算机系统日期和时间格式应该与web服务的格式相同,并且我的转换函数工作正常,但是当我将参数传递给web服务时调用Convert.toDateTime()时,系统会再次将其转换为另一种格式。无论如何,感谢您提供有用的web链接,其中包含了C#中的所有日期/时间格式。