Convert String to Datetime C #

I have a string value that is datetime: "20100825161500" and I want to convert it to a system datetime. I tried Convert.ToDateTime and DateTime.Parse and they do not work.

+5
source share
3 answers

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 in Standard Date and Time Format Strings and Custom Date and Time Format Strings

+14
source

Try using something like this

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

, "format"

+4

Since this line does not have a format recognized by these two functions.

DateTime.Parse and Convert.ToDateTime require proper formatting of the string: http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

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

You will need to write your own parser for such a conversion.

-2
source

All Articles