开发者

Convert String to Datetime C#

开发者 https://www.devze.com 2023-01-14 15:02 出处:网络
I have a string valu开发者_StackOverflow中文版e which is a datetime : \"20100825161500\" and I want to convert this to a System Datetime. I have tried Convert.ToDateTime and DateTime.Parse and these d

I have a string valu开发者_StackOverflow中文版e which is a datetime : "20100825161500" and I want to convert this to a System Datetime. I have tried Convert.ToDateTime and DateTime.Parse and these do not work.


You can use DateTime.ParseExact to pass the format you need.

Here is an example:

var parsed = DateTime.ParseExact("20100825161500","yyyyMMddHHmmss", null);

Possible format values are listed at Standard Date and Time Format Strings and Custom Date and Time Format Strings


Try to use something like this

Datetime D = DateTime.ParseExact("20100825161500","yyyymmdd...",null)

here you have a link about how to make you "format" string


Because this string hasn't a format recognized by these 2 functions.

DateTime.Parse and Convert.ToDateTime require your string to be correctly formatted : http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

http://msdn.microsoft.com/en-us/library/xhz1w05e.aspx

You will have to write your custom parser for this kind of conversion

0

精彩评论

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

关注公众号