我现在在使用 ODBC 连接到 Sybase 数据库的动态 ASP.NET C# Web 应用程序中工作,我的主要问题是应用程序中日期时间和字符串数据类型之间的转换。
数据库以如下形式保存日期时间数据:-
7/11/2011 05:05:05
I think it is in the pattern of "MM/dd/yyyy hh:mm:ss"
我需要调用网络服务并传递一些日期时间参数以获得正确的输出。 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)
将我从数据库中获得的日期转换为适合传递给网络服务的日期,但它总是给我不同的异常(exception)情况,例如:-
异常:System.FormatException:字符串未被识别为有效的日期时间。在 System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) 在 System.DateTime.Parse(String s) 在 WebApplication1.WebForm1.callWebservice()
我不知道我应该如何以正确的方式使用和转换日期,我很失望,因为我尝试了很多示例和函数,但我无法获得正确的方法。
我希望我能找到可以提供帮助的人,在此先感谢......
最佳答案
您应该能够使用 DateTime.ParseExact
尝试以下 DateTime d = DateTime.ParseExact("11/07/11 09:09:09", "dd/MM/yy hh:mm:ss", null);
关于c# - ASP.NET C#中日期和字符串的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6649037/