开发者

The Conversion Between Dates and Strings in ASP.NET C#

开发者 https://www.devze.com 2023-03-19 13:48 出处:网络
I worked now in a dynamic ASP.NET C# web application that connects to a Sybase database using ODBC and my main problem is the conversion between datetime and string data types in my application.

I worked now in a dynamic ASP.NET C# web application that connects to a Sybase database using ODBC and my main problem is the conversion between datetime and string data types in my application.

The database saves the datetime data in the following form:-

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

I need to call a web service and pass some datetime parameters to get the correct output. The static datetime format that is accepted by the web service is:-

11/07/11 09:09:09
I think it is in the pattern of "dd/MM/yy hh:mm:ss"

I tried using the following functions separately

string s = d.toString(string format)
DateTime d = Convert.ToDateTime(string s)
DateTime d = s.Datetime.Parse(string format)
DateTime d = s.Datetime.ParseExact(str开发者_开发问答ing format)

to convert the dates I got from the database to be suitable to be passed to the web service but it always gives me different exceptions like:-

Exception:System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s) at WebApplication1.WebForm1.callWebservice()

I do not know how I should use and convert dates in the proper way and I get disappointed as I try many examples and functions but I can not get the right methodlogy.

I hope that I find someone who could help and Thanks in advance.....


You should be able to use the DateTime.ParseExact

Try the following DateTime d = DateTime.ParseExact("11/07/11 09:09:09", "dd/MM/yy hh:mm:ss", null);


follow the link, there's everything about DateTime Formatting:

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


does your webservice expect a string or an actual datetime object?

Have you tried passing the date time in the format yyyy-MM-dd hh:mm:ss as this format should be universally recognised?

0

精彩评论

暂无评论...
验证码 换一张
取 消