tags:

views:

109

answers:

3

I have a string value 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.

+13  A: 

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

Giorgi
A: 

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

Mike
-1: DateTime.ParseExact allows a format to be specified, you don't need to write this yourself.
Richard
I didn't know ParseExact. Thanks for the info
Mike
+4  A: 

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

Jonathan