Conversion of a object into a particular datetime format
I want to convert a query string object into a datetime in this format:- "YYYY-mm-dd HH:mm:ss.xxx开发者_如何学Go" in C#.net. But when i am using Convert.ToDateTime(object) for getting the datetime value then an exception is being fired.
Could anyone can provide me the iFormatProvider for the same.?
Thanks
Varun Sareen
Have a look at DateTime.TryParseExact Method
I think your problem is trying to convert the QueryString
object, instead of getting a value from the query string and then converting the value.
A QueryString
object is a keyed collection of the values specified in a URL from an HTTP request. So if you have a URL like: http://example.com?a=1&b=2&c=3
, the request QueryString
object will contain three values: 1, 2, and 3. To access the values you would use the keys:
var aValue = Request.QueryString["a"];
And the variable aValue
would then contain the string value "1"
(without the quotes).
After getting the value from the query string, you can use the TryParseExact
method suggested by @astander.
精彩评论