2011-07-11 61 views
2

我现在在一个动态ASP.NET C#Web应用程序中使用ODBC连接到Sybase数据库,我的主要问题是日期时间和字符串数据类型在我的应用程序。ASP.NET C#中日期和字符串之间的转换#

数据库以下形式保存datetime数据: -

7/11/2011 05:05:05 
I think it is in the pattern of "MM/dd/yyyy hh:mm:ss" 

我需要调用Web服务,并通过某些日期时间参数,以获得正确的输出。 由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:字符串未被识别为有效的DateTime。在System.DateTimeParse.Parse(String s,DateTimeFormatInfo dtfi,DateTimeStyles样式)在WebApplication1.WebForm1.callWebservice()的System.DateTime.Parse(String s)

我不知道如何使用和转换日期正确的方法和我感到失望,因为我尝试了很多例子和功能,但我无法得到正确的方法。

我希望我找到谁可以帮助并在此先感谢.....

回答

0

按照链接,有关于日期时间格式的一切:

http://www.csharp-examples.net/string-format-datetime/

+0

谢谢您的回答。我发现我的计算机系统日期和时间格式应该与Web服务的格式相同,我的转换功能工作正常,但是当我将参数传递给Web时调用Convert.toDateTime()时,系统再次将其转换为另一种格式服务。无论如何,感谢您使用C#中所有日期/时间格式的有用网络链接。 – TopDeveloper

0

贵web服务期望的字符串或实际日期对象?

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

2

您应该能够使用DateTime.ParseExact

请尝试以下DateTime d = DateTime.ParseExact("11/07/11 09:09:09", "dd/MM/yy hh:mm:ss", null);